Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本添加到data.frame的一列_R_Dataframe - Fatal编程技术网

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

将文本添加到data.frame的一列,r,dataframe,R,Dataframe,我只想在一列中添加一个文本 我尝试了以下方法(这里是一个示例): 然而,我希望因此: 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

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

我尝试了以下方法(这里是一个示例):

然而,我希望因此:

    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)

dat$addMe您需要的是
sapply
,而不是
lappy

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$addMe <- sapply(dat$addMe, function(x) paste(' Please delete this col!', x))

dat

粘贴已矢量化。与粘贴('message',dat$addMe)
一样,
lappy
返回一个列表,并用该列表替换整个数据帧。您需要
sapply
OP,您的结果帧与实际结果不匹配。Ha。这就解释了:)你根本不需要apply函数是的,我知道,但我也试着在这里遵循他的思维过程。还不完全清楚拉普莱在做什么。
dat$addMe <- paste0("Please delete this col! ", dat$addMe)
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$addMe <- sapply(dat$addMe, function(x) paste(' Please delete this col!', x))

dat
> dat
    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