Tom Cohen
2008-Jan-23 15:30 UTC
[R] help with replacing all comma in a data frame with a dot
Dear list, I have imported a spss data file in R, where a comma is used to separate the decimal numbers, e.g. 3,567 instead of 3.567 as in R. How can I replace the comma with a dot for all values in the data frame >kk a b c d e f 1 ,0925199910320613 82523 8 ,855 ,803 ,69 2 ,278314161923372 91657 26 1,285 1,032 ,823 3 ,278314161923372 91657 26 1,285 1,032 ,823 4 ,278314161923372 91657 26 1,285 1,032 ,823 5 ,278314161923372 91657 26 1,285 1,032 ,823 6 ,278314161923372 91657 26 1,285 1,032 ,823 7 ,203740581833404 72026 94 ,479 ,3 ,061 8 ,243694169416943 23684 77 1,375 ,437 ,054 9 ,3 21857 86 ,829 ,315 ,029 10 ,6 111569 93 ,764 ,4 ,076 11 ,6 111569 93 ,764 ,4 ,076 12 ,6 111569 93 ,764 ,4 ,076 13 ,419788431 35744 95 ,44 ,298 ,076 14 ,419788431 35744 95 ,44 ,298 ,076 16 ,39266161 29098 90 ,361 ,256 ,076 17 ,39266161 29098 90 ,361 ,256 ,076 18 ,691736472 40135 85 ,864 ,284 ,09 19 ,691736472 40135 85 ,864 ,284 ,09 20 ,442407817 48673 86 ,44 ,279 ,088 24 ,0925199910320613 22482 64 ,104 ,082 ,054 Thanks in advance, Tom --------------------------------- Sök efter kärleken! [[alternative HTML version deleted]]
Henrique Dallazuanna
2008-Jan-23 16:00 UTC
[R] help with replacing all comma in a data frame with a dot
apply(apply(df, 2, gsub, patt=",", replace="."), 2, as.numeric) or you can read the data file with read.table using dec argument: df <- read.table('clipboard', dec=",", header=T, row.names=1) On 23/01/2008, Tom Cohen <tom.cohen78 at yahoo.se> wrote:> Dear list, > > I have imported a spss data file in R, where a comma is used to separate the decimal numbers, e.g. 3,567 instead of 3.567 as in R. How can I replace the comma with a dot for all values in the data frame > > >kk > a b c d e f > 1 ,0925199910320613 82523 8 ,855 ,803 ,69 > 2 ,278314161923372 91657 26 1,285 1,032 ,823 > 3 ,278314161923372 91657 26 1,285 1,032 ,823 > 4 ,278314161923372 91657 26 1,285 1,032 ,823 > 5 ,278314161923372 91657 26 1,285 1,032 ,823 > 6 ,278314161923372 91657 26 1,285 1,032 ,823 > 7 ,203740581833404 72026 94 ,479 ,3 ,061 > 8 ,243694169416943 23684 77 1,375 ,437 ,054 > 9 ,3 21857 86 ,829 ,315 ,029 > 10 ,6 111569 93 ,764 ,4 ,076 > 11 ,6 111569 93 ,764 ,4 ,076 > 12 ,6 111569 93 ,764 ,4 ,076 > 13 ,419788431 35744 95 ,44 ,298 ,076 > 14 ,419788431 35744 95 ,44 ,298 ,076 > 16 ,39266161 29098 90 ,361 ,256 ,076 > 17 ,39266161 29098 90 ,361 ,256 ,076 > 18 ,691736472 40135 85 ,864 ,284 ,09 > 19 ,691736472 40135 85 ,864 ,284 ,09 > 20 ,442407817 48673 86 ,44 ,279 ,088 > 24 ,0925199910320613 22482 64 ,104 ,082 ,054 > > Thanks in advance, > Tom > > > --------------------------------- > S?k efter k?rleken! > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Peter Dalgaard
2008-Jan-23 16:02 UTC
[R] help with replacing all comma in a data frame with a dot
Tom Cohen wrote:> Dear list, > > I have imported a spss data file in R, where a comma is used to separate the decimal numbers, e.g. 3,567 instead of 3.567 as in R. How can I replace the comma with a dot for all values in the data frame >How did you read the data in the first place (not by read.spss, I expect)? This is usually more conveniently done on input via the dec= argument to read.table(), or the dedicated delimited-file readers.> > >kk > a b c d e f > 1 ,0925199910320613 82523 8 ,855 ,803 ,69 > 2 ,278314161923372 91657 26 1,285 1,032 ,823 > 3 ,278314161923372 91657 26 1,285 1,032 ,823 > 4 ,278314161923372 91657 26 1,285 1,032 ,823 > 5 ,278314161923372 91657 26 1,285 1,032 ,823 > 6 ,278314161923372 91657 26 1,285 1,032 ,823 > 7 ,203740581833404 72026 94 ,479 ,3 ,061 > 8 ,243694169416943 23684 77 1,375 ,437 ,054 > 9 ,3 21857 86 ,829 ,315 ,029 > 10 ,6 111569 93 ,764 ,4 ,076 > 11 ,6 111569 93 ,764 ,4 ,076 > 12 ,6 111569 93 ,764 ,4 ,076 > 13 ,419788431 35744 95 ,44 ,298 ,076 > 14 ,419788431 35744 95 ,44 ,298 ,076 > 16 ,39266161 29098 90 ,361 ,256 ,076 > 17 ,39266161 29098 90 ,361 ,256 ,076 > 18 ,691736472 40135 85 ,864 ,284 ,09 > 19 ,691736472 40135 85 ,864 ,284 ,09 > 20 ,442407817 48673 86 ,44 ,279 ,088 > 24 ,0925199910320613 22482 64 ,104 ,082 ,054 > > Thanks in advance, > Tom > > > --------------------------------- > S?k efter k?rleken! > > [[alternative HTML version deleted]] > > > ------------------------------------------------------------------------ > > ______________________________________________ > 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. >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
John Kane
2008-Jan-23 19:28 UTC
[R] help with replacing all comma in a data frame with a dot
I think this will do it. mysub <- function(x) {sub(",",".",x)} mydata <- (apply(kk, 2, mysub )) kk1 <- data.frame(apply(mydata, 2, as.numeric)) --- Tom Cohen <tom.cohen78 at yahoo.se> wrote:> Dear list, > > I have imported a spss data file in R, where a > comma is used to separate the decimal numbers, e.g. > 3,567 instead of 3.567 as in R. How can I replace > the comma with a dot for all values in the data > frame > > >kk > a b c d > e f > 1 ,0925199910320613 82523 8 ,855 ,803 ,69 > 2 ,278314161923372 91657 26 1,285 1,032 ,823 > 3 ,278314161923372 91657 26 1,285 1,032 ,823 > 4 ,278314161923372 91657 26 1,285 1,032 ,823 > 5 ,278314161923372 91657 26 1,285 1,032 ,823 > 6 ,278314161923372 91657 26 1,285 1,032 ,823 > 7 ,203740581833404 72026 94 ,479 ,3 ,061 > 8 ,243694169416943 23684 77 1,375 ,437 ,054 > 9 ,3 21857 86 ,829 ,315 ,029 > 10 ,6 111569 93 ,764 ,4 ,076 > 11 ,6 111569 93 ,764 ,4 ,076 > 12 ,6 111569 93 ,764 ,4 ,076 > 13 ,419788431 35744 95 ,44 ,298 ,076 > 14 ,419788431 35744 95 ,44 ,298 ,076 > 16 ,39266161 29098 90 ,361 ,256 ,076 > 17 ,39266161 29098 90 ,361 ,256 ,076 > 18 ,691736472 40135 85 ,864 ,284 ,09 > 19 ,691736472 40135 85 ,864 ,284 ,09 > 20 ,442407817 48673 86 ,44 ,279 ,088 > 24 ,0925199910320613 22482 64 ,104 ,082 ,054 > > Thanks in advance, > Tom > > > --------------------------------- > S???k efter k???rleken! > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. >Looking for the perfect gift? Give the gift of Flickr!