[R] decimal separator from comma to dot

arun smartpink111 at yahoo.com
Fri Aug 9 21:43:55 CEST 2013


Hi,
Try:
 df$POT2_TX<- df$POT_TX
df[,6:7]<- lapply(df[,6:7],function(x) as.numeric(as.character(sub(",",".",x))))
 str(df)
#'data.frame':    10 obs. of  7 variables:
# $ IDANT    : int  37837 37838 37839 37840 37841 37842 37843 40720 40721 40722
# $ N_TX     : int  6 6 6 4 1 1 1 2 2 1
# $ TILT     : int  0 0 0 0 6 6 6 0 0 0
# $ DIREZIONE: int  50 220 110 50 220 110 50 170 70 270
# $ DATA_INI : Factor w/ 2 levels "20/10/2004","29/08/2002": 2 2 2 2 2 2 2 1 1 1
# $ POT_TX   : num  8 8 8 4 11.5 11.5 11.5 10 10 10
# $ POT2_TX  : num  8 8 8 4 11.5 11.5 11.5 10 10 10
A.K.



----- Original Message -----
From: "mbressan at arpa.veneto.it" <mbressan at arpa.veneto.it>
To: David Winsemius <dwinsemius at comcast.net>
Cc: r-help at r-project.org; maxbre <mbressan at arpa.veneto.it>
Sent: Friday, August 9, 2013 3:35 PM
Subject: Re: [R] decimal separator from comma to dot

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