连接列并将其添加到Data Frame的开头 [英] Concatenate columns and add them to beginning of Data Frame
问题描述
Noob这里到R.试图弄清楚一些东西。我需要构建一个向数据集开始添加新列的函数。这个新列是用户指定的其他列中的值的连接。
Noob here to R. Trying to figure something out. I need to build a function that adds a new column to the beginning of a dataset. This new column is a concatenation of the values in other columns that the user specifies.
想象一下,这是名为myDataSet的数据集:
Imagine this is the data set named myDataSet:
col_1 col_2 col_3 col_4
bat red 1 a
cow orange 2 b
dog green 3 c
用户可以使用如下功能:
The user could use the function like so:
addPrimaryKey(myDataSet, cols=c(1,3,4))
得到一个新的数据集的结果,列1,3和4连接成一个名为ID的列,并添加到开头,如下所示:
to get the result of a new data set with columns 1, 3 and 4 concatenated into a column called ID and added to the beginning, like so:
ID col_1 col_2 col_3 col_4
bat1a bat red 1 a
cow2b cow orange 2 b
dog4c dog green 3 c
这是我一直在努力的脚本,但是我一直在盯着它,我想我犯了一些错误。我不知道如何将参数中的列号从粘贴函数中正确地获取。
This is the script I have been working on but I have been staring at it so long, I think I have made a few mistakes. I can't figure out how to get the column numbers from the arguments into the paste function properly.
addPrimaryKey <- function(df, cols=NULL){
newVector = rep(NA, length(cols)) ##initialize vector to length of columns
colsN <- as.numeric(cols)
df <- cbind(ID=paste(
for(i in 1:length(colsN)){
holder <- df[colsN[i]]
holder
}
, sep=""), df) ##concatenate the selected columns and add as ID column to df
df
}
任何帮助将不胜感激。非常感谢
Any help would be greatly appreciated. Thanks so much
推荐答案
paste0
code> do.call :
paste0
works fine, with some help from do.call
:
do.call(paste0, mydf[c(1, 3, 4)])
# [1] "bat1a" "cow2b" "dog3c"
因此,您的函数可以是:
Your function, thus, can be something like:
addPrimaryKey <- function(inDF, cols) {
cbind(ID = do.call(paste0, inDF[cols]),
inDF)
}
您可能还想查看交互
:
interaction(mydf[c(1, 3, 4)], drop=TRUE)
# [1] bat.1.a cow.2.b dog.3.c
# Levels: bat.1.a cow.2.b dog.3.c
这篇关于连接列并将其添加到Data Frame的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!