[R] decimal separator from comma to dot

Rui Barradas ruipbarradas at sapo.pt
Fri Aug 9 21:57:27 CEST 2013


Hello,

To change several columns you can use lapply. In what follows, idx is 
the vector of column numbers, corresponding to the columns you want to 
change:


idx <- 6  # column numbers
df[idx] <- lapply(df[idx], function(x) as.numeric(sub(",", ".", 
as.character(x))))

# check the result
str(df)


Hope this helps,

Rui Barradas


Em 09-08-2013 20:35, mbressan at arpa.veneto.it escreveu:
> This is my reproducible example
>
> df<-structure(list(IDANT = c(37837L, 37838L, 37839L, 37840L, 37841L,
> 37842L, 37843L, 40720L, 40721L, 40722L), N_TX = c(6L, 6L, 6L,
> 4L, 1L, 1L, 1L, 2L, 2L, 1L), TILT = c(0L, 0L, 0L, 0L, 6L, 6L,
> 6L, 0L, 0L, 0L), DIREZIONE = c(50L, 220L, 110L, 50L, 220L, 110L,
> 50L, 170L, 70L, 270L), DATA_INI = structure(c(2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("20/10/2004", "29/08/2002"
> ), class = "factor"), POT_TX = structure(c(4L, 4L, 4L, 3L, 2L,
> 2L, 2L, 1L, 1L, 1L), .Label = c("10", "11,5", "4", "8"), class =
> "factor")), .Names = c("IDANT",
> "N_TX", "TILT", "DIREZIONE", "DATA_INI", "POT_TX"), row.names = c(NA,
> 10L), class = "data.frame")
>
> from david winsemius reply (quite annoyed by me, sorry for that!)
> http://r.789695.n4.nabble.com/decimal-separator-from-comma-to-dot-td4673414.html#a4673461
> and from faq 7.10
> I learnt this is the correct way to perform the conversion from factor to
> number for a single variable
>
> df$POT_TX <- as.numeric(as.character( sub(",", ".", df$POT_TX )))
>
> but what about the case of more than one column (variable) in a data frame?
>
> I hope now my question is fine with the rules of this mailing list
>
> thank you all for the patiente
> I'm learning a lot
>
>
> max
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list