前言
临床上为了方便很多时候大家会使用excel进行数据的存储,使用R对其进行分析,需要将数据导入到R之中,但是R语言目前并不支持直接读入excel数据,本文将为大家介绍多种把excel数据导入R的方法。
Excel转换格式
虽然R语言不能直接读取Excel的数据,但是可以直接读取csv格式的数据,把excel格式数据保存为csv格式数据,就可通过read.csv()
函数进行数据读取。下面展示了R语言官方read.csv
函数的参数(详细内容可在R Studio控制台输入?read.csv
进行查看:
read.csv(file, header = TRUE, sep = ",", quote = "\"",
dec = ".", fill = TRUE, comment.char = "", ...)
使用R语言中的包
虽然R语言目前并不支持直接读入excel数据,但是能够读写excel的包是多种多样的。
readxl包读取excel数据
下框展示了readxl
包读取excel相关函数参数,在控制台中输入??read_excel
查看相关函数及其描述
read_excel(path, sheet = NULL, range = NULL, col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0,
n_max = Inf, guess_max = min(1000, n_max),
progress = readxl_progress(), .name_repair = "unique")
read_xls(path, sheet = NULL, range = NULL, col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0,
n_max = Inf, guess_max = min(1000, n_max),
progress = readxl_progress(), .name_repair = "unique")
read_xlsx(path, sheet = NULL, range = NULL, col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0,
n_max = Inf, guess_max = min(1000, n_max),
progress = readxl_progress(), .name_repair = "unique")
openxlsx包读取excel数据
openxlsx
包不仅能够进行excel表格数据的读入,也能进行excel表格的填写。下面展示R帮助中给出的该函数的参数:
# example
df1 <- read.xlsx(xlsxFile = xlsxFile, sheet = 1,
skipEmptyRows = FALSE)
ofiicer包读取excel数据
read_xlsx(path = NULL)
# example
read_xlsx()
x <- read_xlsx()
print(x, target = tempfile(fileext = ".xlsx"))
xlsx包读取excel数据
file <- system.file("tests", "test_import.xlsx", package = "xlsx")
res <- read.xlsx(file, 1) # read first sheet
head(res)
# NA. Population Income Illiteracy Life.Exp Murder HS.Grad Frost Area
# 1 Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708
# 2 Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
# 3 Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
# 4 Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945
# 5 California 21198 5114 1.1 71.71 10.3 62.6 20 156361
# 6 Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766