【发布时间】:2020-09-28 10:18:53
【问题描述】:
我有一个 df,如果满足条件(如果列包含“你好吗”),我希望 R 在文本前面粘贴“嗨”。
t <- data.frame('x1' = c('how are you','whats up?', 'whats up?'), "x2" = c('how are you','how are you', 'whats up?'), "x3" = c('whats up?','how are you', 'whats up?'))
如何同时为所有列执行此操作? 我试过用 if 句和 lapply
#this doesn't work
t[] <- if(t[] %like% ('how are you')) {paste("Hi, ",t[])}
t[] <- lapply(t, function(x) if(x %like% ('how are you')) {paste("Hi,",x)})
#this works, but then all other content is erased;
t[] <- lapply(t, function(x)
ifelse(x %like% ('how are you'), paste("Hi,", x),""))
提前致谢!
【问题讨论】:
标签: r dataframe if-statement conditional-statements lapply