Regards. I'm a beginner in programing, so I have a basic question for you. If someone could help me please.. I want to create a function, which will be able to export files from excel. I tried with a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE), .. but instead of numbers, it gives me strings for example: "299,311". I can handle this string for example: b <- "299,311" as.numeric(gsub(",", "", b)) 299311 Now, I?m interested how to inport it from that file,. I tried with a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE) a <- gsub(",", "", a) a <- as.numeric(a) But it doesn't work. I used search engine on forum, but didn't find any function that I could help with. I would be very gratefull if someone could help me. Gaja -- View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html Sent from the R help mailing list archive at Nabble.com.
At least post the first 10 lines of the file so that we can see what it is. It may be that some of the data is enclosed in quotes, but it is hard to tell without seeing the actual data. On Fri, Feb 17, 2012 at 12:24 PM, gaja <gajahorvat at hotmail.com> wrote:> Regards. > > I'm a beginner in programing, so I have a basic question for you. > If someone could help me please.. > > I want to create a function, which will be able to export files from excel. > I tried with > a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE), > > .. but instead of numbers, it gives me strings for example: "299,311". > > I can handle this string for example: > b <- "299,311" > as.numeric(gsub(",", "", b)) > 299311 > > Now, I?m interested how to inport it from that file,. > > I tried with > a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE) > a <- gsub(",", "", a) > a <- as.numeric(a) > > But it doesn't work. > I used search engine on forum, but didn't find any function that I could > help with. > > I would be very gratefull if someone could help me. > > Gaja > > -- > View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
If you know all your data should be numeric, you could perhaps try something like this: apply(a, 2, function(x) as.numeric(gsub(" ", "", x))) but it can't be tested without your actual data. (Look at dput() for the best way to send data by email) Michael On Fri, Feb 17, 2012 at 12:24 PM, gaja <gajahorvat at hotmail.com> wrote:> Regards. > > I'm a beginner in programing, so I have a basic question for you. > If someone could help me please.. > > I want to create a function, which will be able to export files from excel. > I tried with > a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE), > > .. but instead of numbers, it gives me strings for example: "299,311". > > I can handle this string for example: > b <- "299,311" > as.numeric(gsub(",", "", b)) > 299311 > > Now, I?m interested how to inport it from that file,. > > I tried with > a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE) > a <- gsub(",", "", a) > a <- as.numeric(a) > > But it doesn't work. > I used search engine on forum, but didn't find any function that I could > help with. > > I would be very gratefull if someone could help me. > > Gaja > > -- > View this message in context: http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Try this: setClass("myClass") setAs("character", "myClass", function(from)as.numeric(gsub(",", "", from))) d <- read.table("clipboard", sep = ",", check.names = FALSE, quote = '"', header = TRUE, fill = TRUE, colClasses = c('character', rep('myClass', 232))) On Fri, Feb 17, 2012 at 3:24 PM, gaja <gajahorvat@hotmail.com> wrote:> Regards. > > I'm a beginner in programing, so I have a basic question for you. > If someone could help me please.. > > I want to create a function, which will be able to export files from excel. > I tried with > a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE), > > .. but instead of numbers, it gives me strings for example: "299,311". > > I can handle this string for example: > b <- "299,311" > as.numeric(gsub(",", "", b)) > 299311 > > Now, I´m interested how to inport it from that file,. > > I tried with > a <- read.csv(file, sep =",", as.is = TRUE, row.names = 1, header = TRUE) > a <- gsub(",", "", a) > a <- as.numeric(a) > > But it doesn't work. > I used search engine on forum, but didn't find any function that I could > help with. > > I would be very gratefull if someone could help me. > > Gaja > > -- > View this message in context: > http://r.789695.n4.nabble.com/editing-import-data-strings-tp4397899p4397899.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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 [[alternative HTML version deleted]]