从xlsx文件读取水平(基于行)数据到R数据帧 [英] Reading horizontal (row-based) data from xlsx files into R data frames

查看:235
本文介绍了从xlsx文件读取水平(基于行)数据到R数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个让我们试试另一种方式的帖子与这一个

This is a "let's try another way" post that is related to this one:

是否可以定义/修改


  • 可以处理存储在 xlsx 文件中的数据是基于行的(即每行代表一个变量)

  • can handle the fact that data stored in an xlsx file is row-based (i.e. each row represents a variable)

并相应地进行转换,以便可以将其存储在基于列的 data.frame (即以前是 xlsx 中的成为

and transforms it accordingly so it can be stored in a column-based data.frame (i.e. what used to be a row in xlsx becomes a column)

捕获基于行的变量的底层类/数据类型


关于 csv 文件我可能会开始转向 readLines ,但不幸的是, xlsx 仍然是一个黑盒子e。

Regarding csv files I would probably start with turning to readLines, but unfortunately xlsx is still a black box to me.

这里有一个 xlsx 文件,其中包含两个数据方向的示例: https://github.com/rappster/stackoverflow/blob/master/ excel / row-and-column-based-data.xlsx

Here's a little xlsx file that features examples for both data orientations: https://github.com/rappster/stackoverflow/blob/master/excel/row-and-column-based-data.xlsx

推荐答案

稍微修改 read.xlsx 函数从 xlsx 包:

library(xlsx)
read.transposed.xlsx <- function(file,sheetIndex) {
        df <- read.xlsx(file, sheetIndex = sheetIndex , header = FALSE)
        dft <- as.data.frame(t(df[-1]), stringsAsFactors = FALSE) 
        names(dft) <- df[,1] 
        dft <- as.data.frame(lapply(dft,type.convert))
        return(dft)            
}

# Let's test it
read.transposed.xlsx("row-and-column-based-data.xlsx", sheetIndex = 2)
#    variable var_1 var_2 var_3
#1 2016-01-01     1     a  TRUE
#2 2016-01-02     2     b FALSE
#3 2016-01-03     3     c  TRUE

这篇关于从xlsx文件读取水平(基于行)数据到R数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆