Home > Software design >  Error in dropping columns - invalid argument to unary operator
Error in dropping columns - invalid argument to unary operator

Time:09-27

I have a data frame that has column names like this:

df =

         01.2020   02.2020   03.2020          
11190      4         1          2
12345      3         3          1
11323      1         2          2

both row-names and colnames are numeric

Now, I want to drop column 1 and 3

Code I used:

df <- subset(df, select = -c("02.2020","01.2020"))

But this throws an error:

invalid argument to unary operator

Can anyone give me an alternative of how to drop these columns

CodePudding user response:

Instead of double quotes, use backquotes (or may use the index i.e. -c(1, 3)

subset(df, select = -c(`02.2020`,`01.2020`))

-output

       03.2020
11190       2
12345       1
11323       2

data

df <- structure(list(`01.2020` = c(4L, 3L, 1L), `02.2020` = c(1L, 3L, 
2L), `03.2020` = c(2L, 1L, 2L)), class = "data.frame", row.names = c("11190", 
"12345", "11323"))
  • Related