Hi all, I have a problem to read csv file with comma as decimal. The numbers were readed as strings. I used the following string in R, but I do not understand why it does not work. tab <- read.csv2("Specimen_RawData_1.csv", header = TRUE, sep = ";", dec ",", nrows = -1) In addition, I copy/past into the post the link to the csv file generated by my instrument. https://dl.dropbox.com/u/95607996/Specimen_RawData_1.csv The file is encoded in ANSI, I tried also the following string, but the result was the same tab <- read.csv2("Specimen_RawData_1.csv", header = TRUE, sep = ";", dec ",", nrows = -1, encoding = "ANSI") The possibility to open the file into a text editor and replace comma with point is a crazy way, because I have around 1000 files to elaborate. The only way for me is to use R. I hope that someone can help me to find the issue. Best, Roberto -- View this message in context: http://r.789695.n4.nabble.com/Problem-to-read-non-standard-csv-file-tp4646018.html Sent from the R help mailing list archive at Nabble.com.
arun kirshna wrote> Hi, > Try this: > tab1<-read.csv2("Specimen_RawData_1.csv",sep="\t",nrows=-1) > tab2<-as.data.frame(sapply(tab1,function(x) > gsub("[,]",".",x)),stringsAsFactors=FALSE) > tab3<-tab2[-1,] > tab3[]<-sapply(tab3,as.numeric) > row.names(tab3)<-1:nrow(tab3) > head(tab3) > # Carico.compressione Corsa.compressione Tempo > Deformazione.in.compressione > #1 0.16648 0.00000 0.000 > 0.00000 > #2 0.16622 0.00000 0.002 > 0.00000 > #3 0.16585 0.00000 0.004 > 0.00000 > #4 3.96132 0.14063 0.104 > 0.01078 > #5 20.67722 0.31937 0.204 > 0.02449 > #6 39.88020 0.49078 0.304 > 0.03763 > str(tab3) > #'data.frame': 81 obs. of 4 variables: > # $ Carico.compressione : num 0.166 0.166 0.166 3.961 20.677 ... > # $ Corsa.compressione : num 0 0 0 0.141 0.319 ... > # $ Tempo : num 0 0.002 0.004 0.104 0.204 0.304 > 0.404 0.504 0.604 0.704 ... > # $ Deformazione.in.compressione: num 0 0 0 0.0108 0.0245 ... > A.K.Thank you very much. It work very well! Best, Roberto -- View this message in context: http://r.789695.n4.nabble.com/Problem-to-read-non-standard-csv-file-tp4646018p4646036.html Sent from the R help mailing list archive at Nabble.com.
On Oct 12, 2012, at 18:45 , Roberto wrote:> Hi all, > I have a problem to read csv file with comma as decimal. The numbers were > readed as strings. > > I used the following string in R, but I do not understand why it does not > work. > tab <- read.csv2("Specimen_RawData_1.csv", header = TRUE, sep = ";", dec > ",", nrows = -1) > > In addition, I copy/past into the post the link to the csv file generated by > my instrument. > > https://dl.dropbox.com/u/95607996/Specimen_RawData_1.csv > > The file is encoded in ANSI, I tried also the following string, but the > result was the same > tab <- read.csv2("Specimen_RawData_1.csv", header = TRUE, sep = ";", dec > ",", nrows = -1, encoding = "ANSI") > > The possibility to open the file into a text editor and replace comma with > point is a crazy way, because I have around 1000 files to elaborate. > The only way for me is to use R. > > I hope that someone can help me to find the issue.Delete 2nd line and use plain read.csv2. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com