0
votes

I have uploaded csv file from Google trends into R and now I am trying to convert the format of the Year column to numeric values as it was identified as character (it was in the format as 2004-01) This is my R code: dataset1$Year <-as.numeric(dataset1$Year) And I receive this warning message: NAs introduced by coercion; and then all the dates are converted to NAs Does anyone know what's the problem is?

1
You may need to convert to Date class and then numeric or extract the first 4 characters as.numeric(substr(dataset1$Year, 1, 4)) - akrun

1 Answers

0
votes

Your read csv function probably converted the column to a factor. You can check if that is true by using the str() on your dataframe and checking the types of all fields.

If your column is a factor, it won't accept any new value beyond those initially established from reading the file. You can keep that from happing by putting the parameter stringsAsFactors = F in the function that reads your csv.