![](/img/trans.png)
[英]Maintain Original Column Names with XLConnect readWorksheet in R
[英]Commas instead of points with XLConnect and readWorksheet in R
我正在使用XLConnect庫讀取.xlsx數據以進行面板數據分析。 我的問題:如果讀取數據幀,則會得到逗號而不是點作為小數點分隔符,並且我不確定為什么會出現這種情況以及如何解決。 我來自歐洲,但是我在Excel中使用小數點。
復制示例非常困難,以下是重要的幾行:
wb = loadWorkbook("Bel_PANEL_DATA.xlsx")
df_price <- readWorksheet(wb, sheet="Prices", keep=c(3,10))
colnames(df_price) <- c("Year", "Price")
一些隨機行的輸出是:
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
還有...
> typeof(df_price$Price)
[1] "character"
如果我使用as.numeric
我只會得到NA值(所有這些值)...
在將它們設置為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.