Converting Character\\Factor to Numeric without NA

2019-08-13 07:53发布

问题:

I am new to R and hope you can help as there seems to be great difficulties with the data mode in R.

I have a csv with reviews and among my columns there is one expressing the data\time of the review. Unfortunately the format is in the form os "x years/months/days ago" and thus I had to convert the column into a date by 1-converting the time into a total amount of days and 2-subtracting the total amount of days from the date I crawled the data.

That's about it. It should be a problem at all but somehow at step #1 when I convert the time given to days to regex so that e.g:

"10 months, 3 weeks ago" turns to "10*30+3*7";

"11 months ago" turns to "11*30";

"1 year, 1 month ago" turns to "1*365+1*30"

  reviews$V6 <- gsub("hour","*0", reviews$V6); as.data.frame(reviews$V6)
  reviews$V6 <- gsub("day","*1", reviews$V6); as.data.frame(reviews$V6)
  reviews$V6 <- gsub("week","*7", reviews$V6); as.data.frame(reviews$V6)
  reviews$V6 <- gsub("month","*30", reviews$V6); as.data.frame(reviews$V6)
  reviews$V6 <- gsub("year","*365", reviews$V6); as.data.frame(reviews$V6)
  reviews$V6 <- gsub(",","+", reviews$V6); as.matrix(reviews$V6)

The outcome is recognized as a character and therefore it doesn't perform the calculation. I tried converting it to numeric by

reviews$date <- as.numeric(as.factor(reviews$date))

but it still doesn't work. Eventually the data convert to numeric but the airthmetic operation is not calculated correctly. I assume because evtlly the operators are not recognized (?)

When looking up:

mode(reviews$date) = 'character"

I looked up several forum entries but they all refer to numeric data with no operators inside and thus I guess this is a special case.