R导入excel数据

前言

临床上为了方便很多时候大家会使用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
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值