在多列中使用grepl和ifelse的简便方法(easier way to use grepl and ifelse across multiple columns)
我有这个名为'jobdata'的数据集
names <- c("person1", "person2", "person3") job1_1_sector <- c("Private", "Public", "Private") job2_1_sector <- c(NA, "Public", "Private") job2_2_sector <- c("Private", "Public", "Other") job3_1_sector <- c("Private", "Private", "Private") job3_2_sector <- c("Other", "Public", "Other") job3_3_sector <- c("Private", NA, "Private") jobs <- cbind(job1_1_sector, job2_1_sector, job2_2_sector, job3_1_sector, job3_2_sector, job3_3_sector ) jobdata <- data.frame(names, jobs)
我想创建一个新的二进制变量
private
,如果跨越相关变量(即job [123] _ [123] _sector),则等于1,如果单词Private出现。 然后另一个为Public
,另一个为Other
。 我已经想出如何在ifelse和grepl中使用它,但看起来我的代码行很长。 有更简单的方法吗?下面这段代码给了我想要的代码:
jobdata$private <- ifelse(grepl("Private", jobdata$job1_1_sector) | grepl("Private", jobdata$job2_1_sector) | grepl("Private", jobdata$job2_2_sector) | grepl("Private", jobdata$job3_1_sector) | grepl("Private", jobdata$job3_2_sector) | grepl("Private", jobdata$job3_3_sector), 1, 0) jobdata$public <- ifelse(grepl("Public", jobdata$job1_1_sector) | grepl("Public", jobdata$job2_1_sector) | grepl("Public", jobdata$job2_2_sector) | grepl("Public", jobdata$job3_1_sector) | grepl("Public", jobdata$job3_2_sector) | grepl("Public", jobdata$job3_3_sector), 1, 0) jobdata$other <- ifelse(grepl("Other", jobdata$job1_1_sector) | grepl("Other", jobdata$job2_1_sector) | grepl("Other", jobdata$job2_2_sector) | grepl("Other", jobdata$job3_1_sector) | grepl("Other", jobdata$job3_2_sector) | grepl("Other", jobdata$job3_3_sector), 1, 0)
谢谢!
I have this dataset called 'jobdata'
names <- c("person1", "person2", "person3") job1_1_sector <- c("Private", "Public", "Private") job2_1_sector <- c(NA, "Public", "Private") job2_2_sector <- c("Private", "Public", "Other") job3_1_sector <- c("Private", "Private", "Private") job3_2_sector <- c("Other", "Public", "Other") job3_3_sector <- c("Private", NA, "Private") jobs <- cbind(job1_1_sector, job2_1_sector, job2_2_sector, job3_1_sector, job3_2_sector, job3_3_sector ) jobdata <- data.frame(names, jobs)
And I want to create a new binary variable
private
that equals 1 if across the relevant variables (that is job[123]_[123]_sector) if the word Private comes up. Then another one forPublic
and another one forOther
. I have figured out how to use this with ifelse and grepl but it looks like my lines of codes are really long. Is there an easier way to do this?This code below gives me the code I want:
jobdata$private <- ifelse(grepl("Private", jobdata$job1_1_sector) | grepl("Private", jobdata$job2_1_sector) | grepl("Private", jobdata$job2_2_sector) | grepl("Private", jobdata$job3_1_sector) | grepl("Private", jobdata$job3_2_sector) | grepl("Private", jobdata$job3_3_sector), 1, 0) jobdata$public <- ifelse(grepl("Public", jobdata$job1_1_sector) | grepl("Public", jobdata$job2_1_sector) | grepl("Public", jobdata$job2_2_sector) | grepl("Public", jobdata$job3_1_sector) | grepl("Public", jobdata$job3_2_sector) | grepl("Public", jobdata$job3_3_sector), 1, 0) jobdata$other <- ifelse(grepl("Other", jobdata$job1_1_sector) | grepl("Other", jobdata$job2_1_sector) | grepl("Other", jobdata$job2_2_sector) | grepl("Other", jobdata$job3_1_sector) | grepl("Other", jobdata$job3_2_sector) | grepl("Other", jobdata$job3_3_sector), 1, 0)
Thanks!
原文:https://stackoverflow.com/questions/50338163
最满意答案
这个
at com.example.hello.SecondActivity.onCreate(SecondActivity.java:58)
说错误是在
SecondActivity.java
第58SecondActivity.java
。 第58行在这里:ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view iv.setImageBitmap(bm); //this is line 58 in this example
所以,看起来像
iv
是null
iv
为null
因为activity_second.xml
不包含id为imageView1
的ImageView要修复,您可以将所有代码移动到
Fragment
类中,也可以将xml移动到activity_main.xml
它可能也值得再次查看
Fragment
文档http://developer.android.com/guide/components/fragments.htmlThis
at com.example.hello.SecondActivity.onCreate(SecondActivity.java:58)
Says that the error is on line 58 of
SecondActivity.java
. Line 58 is here:ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view iv.setImageBitmap(bm); //this is line 58 in this example
So, looks like
iv
isnull
iv
isnull
becauseactivity_second.xml
does not contain an ImageView with the idimageView1
To fix, you can either move all your code into your
Fragment
class, or move the xml into youractivity_main.xml
It's probably also worth looking again into the
Fragment
documentation http://developer.android.com/guide/components/fragments.html
相关问答
更多-
尝试这个。 简单的方法。 let url = NSURL(string:"your url") let imagedata = NSData.init(contentsOf: url! as URL) if imagedata != nil { imageView.image = UIImage(data:imagedata! as Data) } 创建扩展。 extension UIImageView{ f ...
-
在您的Activity中,您正在导入android.support.v4.app.Fragment而您的Fragment是android.app.Fragment 。 这就是为什么你的演员在调用newInstance()之后失败并且你的片段为null。 您的ListFragment 。 您还需要在片段中实现newInstance() 。 public static Fragment newInstance() { FragmentAudio myFragment = new FragmentAud ...
-
Android,图像适配器,getCount()中的空指针异常(Android, Image Adapter, Null Pointer Exception in getCount())[2023-03-01]
在设置图像适配器之前执行此操 if (diff == 1){ gridSize = 16; grid.setNumColumns(4); } if (diff == 2){ gridSize = 25; grid.setNumColumns(5); } if (diff == 3){ gridSize = 36; grid.setNumColumns(6); } 如果它不起作用,请告诉我 do this before set image adapter ... -
您正在ui线程上运行与网络相关的操作。 所以,你得到NetworkOnMainThreadException 。 Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html 您应该使用Thread或Asynctask 。 http://dev ...
-
我忘了从包中重新加载一些所需的变量,包括图像.. 感谢你的帮助! I'd forgotten to reload some of the needed variables from the bundle, including the images.. Thanks for all your help!
-
这一行在这里 btnSongList.setOnClickListener(new OnClickListener(){@Override public void onClick(View v) {visible();}}); 您从未为此特定视图调用findViewById() ,因此指针仍为null。 This line here btnSongList.setOnClickListener(new OnClickListener(){@Override public void on ...
-
当我试图下载图像时,我得到空指针异常...(i am getting null pointer exception when i am trying to download image…)[2024-04-20]
put image = (ImageView)findViewById(R.id.imageView2); 设置内容后查看。 您可以在设置activity的视图后找到一个视图。然后只有它标识id形式的xml布局。 setContentView(R.layout.activity_details); image = (ImageView)findViewById(R.id.imageView2); Put image = (ImageView)findViewById(R.id.imageView2); a ... -
这个 at com.example.hello.SecondActivity.onCreate(SecondActivity.java:58) 说错误是在SecondActivity.java第58 SecondActivity.java 。 第58行在这里: ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view iv.setImageBitmap(bm); //this is line 58 in th ...
-
您正在使用ListView lv; 在课堂上和在创作中 两者都不同。 lv为空 You are using ListView lv; in class as well as within oncreate both are different. lv is null
-
我通过在调用通用图像加载器的displayImage之前添加视图来解决上述错误。 I solved the above error by adding the view before calling the displayImage of universal image loader.