Home > Mobile >  Non Numeric error while creating empty columns in data frame
Non Numeric error while creating empty columns in data frame

Time:10-11

I am trying to create additional empty columns - 104 columns - in an existing data frame the class of data frame is as follows: "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"))

It has columns which are "chr", integer64, and Date classes

My code for creating empty column is as follows:

         df1[15:118] <- sapply(1:104, " ", df1[[1]])

The code is giving out following error:

         Error in FUN(X[[i]], ...) : non-numeric argument to binary operator

Please help What error I m doing

CodePudding user response:

Your ROW_ID is defined as character and NOT a number. Hence the is failing

Just change

ROW_ID = c("23416","23416","23416")

to

ROW_ID = c(23416,23416,23416)

your code will work

Full code is here:

  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]])
  •  Tags:  
  • r
  • Related