将基于共享根的相似变量名称粘贴到另一个变量中

时间:2018-07-24 11:45:59

标签: r string dataframe

如果要在第一个数据帧中找到类似名称的var的根名,我想将在一个数据帧(df1)中找到的相似的var名称粘贴到另一个数据帧的var(df2 $ varconsistsof)中在第二个数据帧中的var(df2 $ var)中。

感谢您的帮助!

# Starting point 
df1 <- data.frame(abc_book_E1_C1=c(1:5),abc_book_E1_C2=c(1:5),abc_book_E1_C3=c(1:5),def_book_E1_C1=c(1:5),abc_toy_E1_R1=c(1:5),abc_toy_E1_R2=c(1:5)) 
df2 <- data.frame(var=c("abc_book","abc_toy"),varconsistsof=c(NA,NA))

# Goal
dfgoal <- data.frame(var=c("abc_book","abc_toy"),varconsistsof=c("abc_book_E1_C1,abc_book_E1_C2,abc_book_E1_C3","abc_toy_E1_R1,abc_toy_E1_R2"))

# Attempts
df2$varorigin <- grep(df2$var,names(df),value=TRUE) 
df2$varorigin <-  apply(df1, 1, function(df2$var) grep(df2$var,names(df),value=TRUE))

1 个答案:

答案 0 :(得分:1)

您可以尝试:

df2$varconsistsof = lapply(df2$var, function(x){colnames(df1)[grepl(x,colnames(df1))]})

输出:

       var                                  varconsistsof
1 abc_book abc_book_E1_C1, abc_book_E1_C2, abc_book_E1_C3
2  abc_toy                   abc_toy_E1_R1, abc_toy_E1_R2

请注意,现在str(df2)返回了

'data.frame':   2 obs. of  2 variables:
 $ var          : Factor w/ 2 levels "abc_book","abc_toy": 1 2
 $ varconsistsof:List of 2
  ..$ : chr  "abc_book_E1_C1" "abc_book_E1_C2" "abc_book_E1_C3"
  ..$ : chr  "abc_toy_E1_R1" "abc_toy_E1_R2"

并将第二列简化为简单的字符串,您可以使用sapply并将该语句包装在带有参数paste的{​​{1}}中,因此:

collapse=',

现在df2$varconsistsof = sapply(df2$var, function(x){ paste(colnames(df1)[grepl(x,colnames(df1))],collapse=',')}) 返回

str(df2)

希望这会有所帮助!