如果column包含字符串,如何将列标题添加到新列

时间:2019-02-06 07:51:53

标签: r

帮助!我正在尝试合并两个文件(行数均相等于〜4,000),但是其中一个具有摘要列,而另一个则没有,并且我不确定该如何处理。抱歉,我是R的新手。

我的逻辑是文件B上的IF语句,如果该列等于“是”,则将列标题打印到一个新列(也称为“ All_results”),然后通过“ ID”和“ All_results”合并两个文件。有建议的R函数可以做到这一点吗?

*******文件A ********

ID  All_results  cat  dog  fly
1  [cat, dog]     yes  yes  no
2  [cat]          yes  no  no
3  [cat, fly]     yes  no  yes
4  [dog]          no  yes  no
5  [dog, fly]     no  yes  yes

*****文件B ******

ID  lion  rat  frog
1   yes   yes  no
2   yes   no   no
3   yes   no   yes
4   no    yes  no
5   no    yes  yes

预期结果 ****** Merged_files ********

ID  All_results         cat  dog  fly  lion  rat  frog
1  [cat,dog,lion,rat]   yes  yes  no    yes  yes  no
2  [cat,fly]            yes  no   no    yes  no   no
3  [cat, fly,lion,frog] yes  no   yes   yes  no   yes
4  [dog,rat]            no   yes  no    no   yes  no
5  [dog, fly,rat,frog]  no   yes  yes   no   yes  yes

1 个答案:

答案 0 :(得分:1)

我自己不是R方面的专家,但这给了我想要的结果。希望对您有所帮助。

filea <- read.table( text = "ID  All_results  cat  dog  fly
1  \"[cat, dog]\"     yes  yes  no
2  \"[cat]\"          yes  no  no
3  \"[cat, fly]\"     yes  no  yes
4  \"[dog]\"          no  yes  no
5  \"[dog, fly]\"     no  yes  yes", header = TRUE)

fileb <- read.table(text = "ID  lion  rat  frog
                    1   yes   yes  no
                    2   yes   no   no
                    3   yes   no   yes
                    4   no    yes  no
                    5   no    yes  yes", header = TRUE)

fileab <- merge(filea[,-c(2)], fileb, by = "ID")

fileab$All_results <-  apply(fileab, 1, 
function(x) paste("[", paste(colnames(fileab)[x == "yes"], collapse = ","), "]", sep = ""))

产生此结果

ID cat dog fly lion rat frog         All_results
1 yes yes  no  yes yes   no  [cat,dog,lion,rat]
2 yes  no  no  yes  no   no          [cat,lion]
3 yes  no yes  yes  no  yes [cat,fly,lion,frog]
4  no yes  no   no yes   no           [dog,rat]
5  no yes yes   no yes  yes  [dog,fly,rat,frog]

不区分大小写的更新:

插入此内容:grepl("yes", x, ignore.case = T)

fileab$All_results <-  apply(fileab, 1, function(x) paste("[", 
paste(colnames(fileab)[grepl("yes", x, ignore.case = T)], collapse = ","), "]", sep = ""))