read_excel 不会 trim 空格
read_excel won't trim whitespace
我正在使用包 readxl 加载 excel 文件。默认情况下,它应该去除白色 space 但它没有这样做。
文件可以直接从下面的link下载,也可以通过附录B
的网站下载
http://www2.nationalgrid.com/WorkArea/DownloadAsset.aspx?id=8589937799
require(readxl);require(tidyverse)
test <- read_excel("ETYS 2016 Appendix B.xlsx", skip = 1, sheet = 22, trim_ws = TRUE)
print(test$`MVAr Generation`)
test$`MVAr Generation` %>% str_count(patter = "\s")
test$`MVAr Generation` %>% table #all are numeric
test$`MVAr Generation` %>% class #however the class is characer
test$`MVAr Generation` %>% str_count(patter = "\s") %>%
sum(na.rm = T) #It should be 0 however it is 2
这个问题导致分析出现问题,从这个例子中可以看出,其中数字列是一个字符。
帮助将不胜感激
library(readxl)
readxl::excel_sheets('ETYS 2016 Appendix B.xlsx')[22]
test <- read_excel("ETYS 2016 Appendix B.xlsx", skip = 1, sheet = 22,
trim_ws = FALSE)
test$`MVAr Generation` <- as.numeric(gsub('^\s', "", test$`MVAr Generation`))
错误可能是由于字符编码引起的。当我强制对列进行数字解释时出现此错误:
Expecting numeric in D9 / R9C4: got 'Â 225'
您可以通过将前导空格替换为 gsub
来手动避免这种情况。
也许这就是您想要的:
library(xlsx)
test <- read.xlsx("ETYS 2016 Appendix B.xlsx", sheetName = 22,
colIndex = 1:7, startRow = 2, header = TRUE,
stringsAsFactors = FALSE)
# remove whitespace
test <- data.frame(lapply(test, function(y) {
y <- gsub("^\s+", "", y);
y <- gsub("Â", "", y); y
y <- gsub("^\s+", "", y);
}))
# set tidy cols to numeric
cols = c(3, 4, 5, 7)
test[,cols] = apply(test[,cols], 2, function(x) as.numeric(x))
# test
class(test$Unit.Number)
test$MVAr.Absorption
@troh 对字符编码的洞察让我开始考虑使用正则表达式。 @jaySF 在整个数据框中的应用是同时处理所有列的好方法。这两个建议使我得出以下答案。
require(dplyr);require(purrr);require(readr)
RemoveSymbols <-function(df) {
df %>% mutate_all( funs(gsub("^[^A-Z0-9]", "", ., ignore.case = FALSE))) %>%
map_df(parse_guess)
}
test2 <- RemoveSymbols(test)
sapply(test2,class)
我正在使用包 readxl 加载 excel 文件。默认情况下,它应该去除白色 space 但它没有这样做。
文件可以直接从下面的link下载,也可以通过附录B
的网站下载http://www2.nationalgrid.com/WorkArea/DownloadAsset.aspx?id=8589937799
require(readxl);require(tidyverse)
test <- read_excel("ETYS 2016 Appendix B.xlsx", skip = 1, sheet = 22, trim_ws = TRUE)
print(test$`MVAr Generation`)
test$`MVAr Generation` %>% str_count(patter = "\s")
test$`MVAr Generation` %>% table #all are numeric
test$`MVAr Generation` %>% class #however the class is characer
test$`MVAr Generation` %>% str_count(patter = "\s") %>%
sum(na.rm = T) #It should be 0 however it is 2
这个问题导致分析出现问题,从这个例子中可以看出,其中数字列是一个字符。 帮助将不胜感激
library(readxl)
readxl::excel_sheets('ETYS 2016 Appendix B.xlsx')[22]
test <- read_excel("ETYS 2016 Appendix B.xlsx", skip = 1, sheet = 22,
trim_ws = FALSE)
test$`MVAr Generation` <- as.numeric(gsub('^\s', "", test$`MVAr Generation`))
错误可能是由于字符编码引起的。当我强制对列进行数字解释时出现此错误:
Expecting numeric in D9 / R9C4: got 'Â 225'
您可以通过将前导空格替换为 gsub
来手动避免这种情况。
也许这就是您想要的:
library(xlsx)
test <- read.xlsx("ETYS 2016 Appendix B.xlsx", sheetName = 22,
colIndex = 1:7, startRow = 2, header = TRUE,
stringsAsFactors = FALSE)
# remove whitespace
test <- data.frame(lapply(test, function(y) {
y <- gsub("^\s+", "", y);
y <- gsub("Â", "", y); y
y <- gsub("^\s+", "", y);
}))
# set tidy cols to numeric
cols = c(3, 4, 5, 7)
test[,cols] = apply(test[,cols], 2, function(x) as.numeric(x))
# test
class(test$Unit.Number)
test$MVAr.Absorption
@troh 对字符编码的洞察让我开始考虑使用正则表达式。 @jaySF 在整个数据框中的应用是同时处理所有列的好方法。这两个建议使我得出以下答案。
require(dplyr);require(purrr);require(readr)
RemoveSymbols <-function(df) {
df %>% mutate_all( funs(gsub("^[^A-Z0-9]", "", ., ignore.case = FALSE))) %>%
map_df(parse_guess)
}
test2 <- RemoveSymbols(test)
sapply(test2,class)