[英]Commas instead of points with XLConnect and readWorksheet in R
I am using the XLConnect library to read .xlsx data for paneldata analysis purposes. 我正在使用XLConnect库读取.xlsx数据以进行面板数据分析。 My problem: If reading a data frame, I get commas instead of points as a decimal separator and I am not sure why this is the case and how I should solve it.
我的问题:如果读取数据帧,则会得到逗号而不是点作为小数点分隔符,并且我不确定为什么会出现这种情况以及如何解决。 I am from Europe, but I use a decimal point in Excel.
我来自欧洲,但是我在Excel中使用小数点。
Reproducing an example is quite difficult, here are the important lines: 复制示例非常困难,以下是重要的几行:
wb = loadWorkbook("Bel_PANEL_DATA.xlsx")
df_price <- readWorksheet(wb, sheet="Prices", keep=c(3,10))
colnames(df_price) <- c("Year", "Price")
The output of some random lines is: 一些随机行的输出是:
Year Price
38 2000 175,1735
39 2001 196,2913
40 2002 204,3013
41 2003 251,2955
42 2004 259,8135
43 2005 265,1185
44 2006 370,9554
45 2007 367,2868
46 2008 339,0321
47 2009 348,6053
and ... 还有...
> typeof(df_price$Price)
[1] "character"
If I use as.numeric
I only get NA values (all of them)... 如果我使用
as.numeric
我只会得到NA值(所有这些值)...
Before setting them to as.numeric()
, you'd want to do substitution of ","
to "."
在将它们设置为
as.numeric()
,您需要将","
替换为"."
: :
df_price$Price <- as.numeric(sub(",", ".", df_price$Price))
data<-read.table(header=T,text="
Year Price
2000 175,1735
2001 196,2913
2002 204,3013
2003 251,2955
2004 259,8135
2005 265,1185
2006 370,9554
2007 367,2868
2008 339,0321
2009 348,6053")
price<-paste(substr(data$Price,1,3),substr(data$Price,5,8))
library(stringr)
data$Price <- as.numeric(str_replace_all(price, fixed(" "), ""))
str(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.