【发布时间】:2017-04-20 19:21:15
【问题描述】:
我的 R 代码
data <- read.csv('filename.csv')
typof(data)
[1] "list"
str(data)
'data frame' : 9 obs. of 10 variables
$Name: Factor w/9 levels "Name 1", "Name 2",....
$Column2: chr "","Text1","","Text2"
$Column3: chr "Text2","Text3","","Text1"
$Column4: chr "","","","Text1"
#and so on
要求:
我想要的只是$Column2,$Column3,$Column4,...只要有非空白值,添加前缀(Here this is)和后缀(completed)。
所以考虑到上面的data Column2,第二行现在有值"Text1"应该变成"Here this is Text1 completed."
Column3: 1st, 2nd and 4th cell 中同样需要添加前缀和后缀值。
我不想使用循环,除非需要/必要。
我的尝试:
我尝试了一些尝试,例如interaction、mget、append 等等,但似乎没有任何效果。
【问题讨论】:
-
你试过mutate_each和paste吗?
-
看我的回答,它在一行代码中完成,它也经过测试。
-
@ZeeshanArif 您的答案针对单个列,而不是整个数据集。此外,已经发布了类似的答案(也在列上循环)
标签: r