我试图在现有数据框架中创建额外的空列- 104列数据帧的类如下:"tbl_df"tbl"data.frame">
df1 <- data.frame(ROW_ID = c("23416","23416","23416"),
Process_ID = c("SLT","SLT","SLT"),
Operation_Code = c("SLT","SLT","SLT"),
Resource_Group_Code = c("BD","BD","BT"),
Location_Code = c("JS","JS","JS"),
Resource_Code = c("B-T234","B-T234","B-T234"),
Resource_Desc = c("699","699","699"),
iDeleteFlag = c("N","N","N"),
Attribute_Code = c("RA002","RA002","RA002"),
Attribute_Value = c("266","269","298"),
Capacity_Type = c("s","s","s"),
Planning_Version = c("PDMT","PDMT","PDMT"),
Valid_From = c("2012-02-01", "2012-03-01", "2012-04-01"),
Valid_To = c("2012-07-01", "2012-08-01", "2012-09-01"))
它的列是"chr", integer64和Date类
我创建空列的代码如下:df1[15:118] <- sapply(1:104, "+", df1[[1]])
代码给出如下错误:
Error in FUN(X[[i]], ...) : non-numeric argument to binary operator
请帮助我在做什么错误
您的ROW_ID被定义为字符而不是数字。因此+是失败的
只改变
ROW_ID = c("23416","23416","23416")
ROW_ID = c(23416,23416,23416)
你的代码将工作
完整代码在这里:
df1 <- data.frame(ROW_ID = c(23416,23416,23416),
Process_ID = c("SLT","SLT","SLT"),
Operation_Code = c("SLT","SLT","SLT"),
Resource_Group_Code = c("BD","BD","BT"),
Location_Code = c("JS","JS","JS"),
Resource_Code = c("B-T234","B-T234","B-T234"),
Resource_Desc = c("699","699","699"),
iDeleteFlag = c("N","N","N"),
Attribute_Code = c("RA002","RA002","RA002"),
Attribute_Value = c("266","269","298"),
Capacity_Type = c("s","s","s"),
Planning_Version = c("PDMT","PDMT","PDMT"),
Valid_From = c("2012-02-01", "2012-03-01", "2012-04-01"),
Valid_To = c("2012-07-01", "2012-08-01", "2012-09-01"))
df1[15:118] <- sapply(1:104, "+", df1[[1]])