0

I am using subset and select to extract columns from a dataframe. My code is simple, however errors occur.

names(df7)
df7 <- subset(df7, select = -c("Parameter.Code", "Time.Local", "Date.GMT", "Time.GMT", "Sample.Measurement"))

below is the results and the errors.

names(df7)
 [1] "Latitude"            "season.x"            "AOT40_1"             "count1"              "count_full"         
 [6] "AOT40_2"             "State.Code"          "County.Code"         "Site.Num"            "Parameter.Code"     
[11] "POC"                 "Longitude"           "Datum"               "Parameter.Name"      "Date.Local"         
[16] "Time.Local"          "Date.GMT"            "Time.GMT"            "Sample.Measurement"  "Units.of.Measure"   
[21] "MDL"                 "Uncertainty"         "Qualifier"           "Method.Type"         "Method.Code"        
[26] "Method.Name"         "State.Name"          "County.Name"         "Date.of.Last.Change" "AOT40"              
[31] "season.y"

df7 <- subset(df7, select = -c("Parameter.Code", "Time.Local", "Date.GMT", "Time.GMT", "Sample.Measurement"))
Error in -c("Parameter.Code", "Time.Local", "Date.GMT", "Time.GMT", "Sample.Measurement") : 
  invalid argument to unary operator

Any ideas???

Yabin Da
  • 553
  • 5
  • 11
  • 1
    Remove double quotes in the `c` vector. For special names (chars, spaces, etc.) enclose each with backticks. – Parfait Jun 06 '19 at 21:24
  • But I did see someone using double quotes referring column names when selecting, like c("A", "B"), where A and B are column names. – Yabin Da Jun 06 '19 at 21:58
  • Using `subset`? See 8-year old dup link where one of the [answers](https://stackoverflow.com/a/17796442/1422451) even discuss quotes will not work. Nonetheless, even docs (`?subset` ) warn this method is *a convenience function intended for use interactively* and can have *unanticipated consequences*. Use `[` instead for rows/cols filter. – Parfait Jun 07 '19 at 00:53
  • Totally make sense. Thanks. – Yabin Da Jun 07 '19 at 01:38

0 Answers0