Unsuccessful change from character to numeric

I have this df:

structure(list(year = c("1970", "1971", "1971", "1971", "2001", 
"2001", "2001", "2001", "2005", "2009"), mes = c("12", "01", 
"01", "01", "12", "12", "12", "12", "12", "12"), día = c("30", 
"01", "02", "04", "16", "17", "24", "25", "24", "20"),  tmed = c(-3.3, 
-3.6, -4.8, -5.8, -2, 0.6, -1.2, -2.4, -1.9, -3.5), prec = c("0,7", 
"0,0", "0,0", "Ip", "0,0", "0,0", "0,0", "Ip", "0,0", "Ip"), 
    tmin = c(-8.6, -8.5, -8.5, -10.4, -9, -9.4, -9.2, -9.6, -8.6, 
    -8.6), horatmin = c("04:30", "05:00", "09:00", "08:00", "08:00", 
    "Varias", "08:05", "08:00", "08:40", "08:12"), tmax = c(2, 
    1.2, -1, -1.2, 5, 10.6, 6.8, 4.8, 4.8, 1.6), horatmax = c("16:00", 
    "16:00", "16:00", "17:00", "16:00", "15:50", "15:10", "15:10", 
    "16:10", "15:07"), sol = c(NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, 7.8), velmedia = c(0.6, 0, 0.6, 0, 1.1, 1.4, 0.8, 0.8, 
    0.3, 1.9), presMax = c(945.2, 958.4, 958.8, 956, 960.3, 960.3, 
    956.3, 955.9, 960.6, 952.5), horaPresMax = c("24", "23", 
    "10", "11", "Varias", "00", "10", "10", "10", "03"), presMin = c(936.8, 
    950.6, 952.5, 952.4, 957.9, 955.8, 953.9, 952.9, 957.3, 939.4
    ), horaPresMin = c("12", "00", "24", "00", "15", "16", "15", 
    "16", "18", "24"), dir = c(NA, NA, NA, NA, "11", "07", "11", 
    "11", "28", "08"), racha = c(NA, NA, NA, NA, 3.1, 6.4, 3.6, 
    3.9, 3.1, 6.9), horaracha = c(NA, NA, NA, NA, "18:58", "16:54", 
    "20:54", "21:44", "12:22", "17:49")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L))

The "prec" column is in character format. If I do df$prec <- as.numeric(df$prec) all the data is converted to NA. I thought it might be because of the "Ip" things in the column, but if I do df$prec[df$prec == "Ip"] <- 0 and then again df$prec <- as.numeric(df$prec) I obtain same results.

Any idea?

It works for me to replace the commas with periods and then do as.numeric().

DF$prec <- as.numeric(sub(",",".",DF$prec))

Also for me. Thanks!

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.