R:将数据帧转换为三维数组? [英] R: Transform dataframe into three dimensional arrays?

查看:51
本文介绍了R:将数据帧转换为三维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个数据帧,例如

I would like to transform a data frame e.g.

day <- c(1,1,1,2,2,2,3,3,3)
area <-c("A","B","C")
area <- rep(area,3)
measure1 <- c(99,97,95,102,105,98,103,98,90)
measure2 <- c(3,4,78,2,5,9,7,4,3)
A_dataframe <-data.frame(
  day,area,measure1,measure2
)

分为三个三维数组,其中第一维为Area:"A","B","C",而第二维和第3维包含day作为行,measure1& measure2作为列,分别对应于其区域"A","B"或"C"?

into three dimensional arrays where the first dimension is Area: "A", "B", "C" while 2nd and 3rd dimensions containing day as row and measure1&measure2 as column corresponding to its area "A", "B" or "C"?

我熟悉Matlab中的单元数组,因此想知道R是否可以执行类似的操作.

I am familiar with cell array in Matlab so wondering if R can perform something similar.

我尝试过融化"和播出",但都没有成功.

I have tried "melt" and "dcast" but none is successful.

感激.

推荐答案

您可以使用 dplyr 包中的 group_split():

df_to_list <- A_dataframe %>% 
  dplyr::group_split(area)

names(df_to_list) <- c("a", "b", "c")

但是,我不确定您所说的三维"到底是什么意思.

However, I am not quite sure what you mean by "3rd dimension" exactly.

这篇关于R:将数据帧转换为三维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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