Home > Back-end >  How to drop a Date column in R
How to drop a Date column in R

Time:11-05

I have a date-time stamp column and i was able to convert the column to R date-time format, in a bid for me to split the column, i created 2 new columns with the date information in one and time information in the order. Now I have found a way to split the original Date...Timestamp column so i want to drop the 2 new columns but I'm getting this error.

I tried this code:
newpHdata = subset(newpHdata, select = -c(newpHdata$Dates))
newpHdata = subset(newpHdata, select = -c(newpHdata$Time))

And I'm getting this error
> newpHdata = subset(newpHdata, select = -c(newpHdata$Dates))
Error in `-.Date`(c(newpHdata$Dates)) : 
  unary - is not defined for "Date" objects
> newpHdata = subset(newpHdata, select = -c(newpHdata$Time))
Error in -c(newpHdata$Time) : invalid argument to unary operator

CodePudding user response:

Use the column name alone in subset without any $ as the $ extracts the values of the columns whereas we need only the column names

newpHdata <- subset(newpHdata, select = -c(Dates))

-testing

newpHdata <- head(iris)
newpHdata$Dates <- Sys.Date()
subset(newpHdata, select = -c(Dates))
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
 subset(newpHdata, select = -c(newpHdata$Dates))
Error in `-.Date`(c(newpHdata$Dates)) : 
  unary - is not defined for "Date" objects
  • Related