Convert from Character (from a list) to Numeric or Double

Good Morning Guys, i hope you're safe.

I was wondering if there was a way to convert a "character" data frame (composed by two lists), into a "numeric" vector (double).
I have hourly data series from 2012 to 2020 with related rainfall measures (with value=0 where I have no data) and just I want to convert the rainfall columns to "numeric" because i need to extract the MEAN for different ranges of time and obvs with "character" is not working.

I tried "lapply", "as.numeric", "as.numeric(as.character(...)" but i got nothing.
Anytime I try to convert, I got a vector filled by NA.

have a look to the code



start_date <- as.POSIXct(paste0("2012/01/01", "00:00:00"), format= "%Y/%m/%d %H:%M:%S")
end_date <- as.POSIXct(paste0("2020/01/01","00:00:01"), format= "%Y/%m/%d %H:%M:%S")
tot_date <- seq.POSIXt(start_date, end_date, by = "5 min")
zero_output <-data.frame(tot_date)

rio_5min <- import("____pluvio.txt")
zero_output$tot_date <- as.character(zero_output$tot_date)
rio_5min$date <- as.character(rio_5min$date)
AD <- left_join(zero_output,rio_5min,by=c("tot_date"="date"))  
AD <- data.frame(AD)

#rio_5min$rain <- as.numeric(rio_5min$rain)
#ADr <- AD$rain
#ADr <- as.numeric(gsub(" ","",ADr))

a <-integer(841541)

for(i in seq_along(a)) 
  cin[i] <- mean(AD$rain[i:(i+9)]) 


sort(cin, decreasing=TRUE)


How to resolve this? thank You so much.

Hi :slight_smile:

If you can provide a reprex people will be able to help you easily: FAQ: How to do a minimal reproducible example ( reprex ) for beginners

1 Like

You're right, my bad, im gonna add it right now, thank You

I found the error, the input file has the "," as a separator of decimal instead of the "."
so trying to convert from character to numeric I got errors.

Very entry level error,

Thank You all Guys

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.