当前位置:  开发笔记 > 编程语言 > 正文

将文本添加到data.frame的一列

如何解决《将文本添加到data.frame的一列》经验,为你挑选了1个好方法。

我想只在一列添加一个文本.

我尝试了以下(这里作为一个例子):

employee <- c('John Doe','Peter Gynn','Jolie Hope')
addMe <- c('test1','test2','test3')
salary <- c(21000, 23400, 26800)
dat <- data.frame(employee, salary, addMe)

dat[] <- lapply(dat$addMe, function(x) paste(' Please delete this col!', x))

但是,我的结果框架如下所示:

     employee                            salary                          addMe
1    Please delete this col! John Doe    Please delete this col! 21000   Please delete this col! test1
2    Please delete this col! Peter Gynn  Please delete this col! 23400   Please delete this col! test2
3    Please delete this col! Jolie Hope  Please delete this col! 26800   Please delete this col! test3

但是我希望得到一个结果:

    employee      salary        addMe
1   John Doe      21000         Please delete this col! test1
2   Peter Gynn    23400         Please delete this col! test2
3   Jolie Hope    26800         Please delete this col! test3    

有什么建议我做错了吗?

感谢您的回复!



1> doctorG..:

赶紧跑

dat$addMe <- paste0("Please delete this col! ", dat$addMe)

代替你的lapply线.

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有