gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:28:08 24 4
gpt4 key购买 nike

我只想将文本添加到一列。

我尝试了以下(这里作为示例):

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

任何建议我做错了什么?

感谢您的回复!

最佳答案

快跑

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

代替你的 lapply 线。

关于r - 将文本添加到 data.frame 的一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34594846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com