简体   繁体   English

R:读入CSV小数点和逗号

[英]R: Read in CSV decimal point and comma

Im a beginner, i want to read in a csv file with both . 我是初学者,我想用两者读取csv文件。 and , being a decimal separator. 并且是小数点分隔符。 how can i do this in R. thanks 我怎么能在R.谢谢

AllDataxx=read.csv("C:Sample.csv",
                  header=TRUE,sep=";",dec=", & .")

You cannot do this out of the box I am afraid. 你恐怕不能开箱即用。 Of course what you can do is decide which you want say . 当然,你可以做的是决定你想说什么. , and use the colClasses argument to load the , columns as a character . ,并使用colClasses参数来加载,列作为一个character Then you will use gsub(pattern= , ,replacement='.', x=yourColumnVector) to change the , into . 然后你将使用gsub(pattern= ,replacement='.', x=yourColumnVector)来改变,进入. and as.numeric to cast the vector to numeric as.numeric将向量转换为numeric

DF = data.frame(a=c(1.1,1.3,1.4),b=c('1,1','1,3','1,6'))
DF
    a   b
1 1.1 1,1
2 1.3 1,3
3 1.4 1,6
str(DF)
'data.frame':   3 obs. of  2 variables:
 $ a: num  1.1 1.3 1.4
 $ b: chr  "1,1" "1,3" "1,6"
DF$b = as.numeric(gsub(',','.',DF$b))
DF
    a   b
1 1.1 1.1
2 1.3 1.3
3 1.4 1.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM