R 将数据重塑为面板形式
我有一些数据,其中对象名是一个变量名,如R 将数据重塑为面板形式,r,csv,format,R,Csv,Format,我有一些数据,其中对象名是一个变量名,如EPS,price等(大约25个这样的不同对象) 数据的排列方式如下: EPS <- read.table(text = " Year Microsoft Facebook 2001 12 20 2002 15 23 2003 16 19 ", header = TRUE) Profit <- read.table(text = " Year Micros
EPS
,price
等(大约25个这样的不同对象)
数据的排列方式如下:
EPS <- read.table(text = "
Year Microsoft Facebook
2001 12 20
2002 15 23
2003 16 19
", header = TRUE)
Profit <- read.table(text = "
Year Microsoft Facebook
2001 15 36
2002 19 40
2003 25 45
", header = TRUE)
如何做到这一点?有没有办法将所有变量的数据安排为一个对象?每个变量的数据从一个csv文件(如EPS.csv、Profit.csv等)导入到R中。有没有办法创建从导入到以所需格式排列数据的循环 我们可以在
列表中获取数据集。如果我们已经创建了“EPS”、“利润”作为对象,请使用mget
获取列表中的对象,使用rbindlist
将其转换为单个data.table,melt
转换为long
格式,并使用dcast
将其重塑为“宽”
library(data.table)#v1.9.6+
DT <- rbindlist(mget(c('EPS', 'Profit')), idcol=TRUE)
DT1 <- dcast(melt(rbindlist(mget(c('EPS', 'Profit')), idcol=TRUE),
id.var=c('.id', 'Year'), variable.name='Co_Name'),
Year+Co_Name~.id, value.var='value')
DT1
# Year Co_Name EPS Profit
#1: 2001 Microsoft 12 15
#2: 2001 Facebook 20 36
#3: 2002 Microsoft 15 19
#4: 2002 Facebook 23 40
#5: 2003 Microsoft 16 25
#6: 2003 Facebook 19 45
我们可以在列表中获取数据集。如果我们已经创建了“EPS”、“利润”作为对象,请使用mget
获取列表中的对象,使用rbindlist
将其转换为单个data.table,melt
转换为long
格式,并使用dcast
将其重塑为“宽”
library(data.table)#v1.9.6+
DT <- rbindlist(mget(c('EPS', 'Profit')), idcol=TRUE)
DT1 <- dcast(melt(rbindlist(mget(c('EPS', 'Profit')), idcol=TRUE),
id.var=c('.id', 'Year'), variable.name='Co_Name'),
Year+Co_Name~.id, value.var='value')
DT1
# Year Co_Name EPS Profit
#1: 2001 Microsoft 12 15
#2: 2001 Facebook 20 36
#3: 2002 Microsoft 15 19
#4: 2002 Facebook 23 40
#5: 2003 Microsoft 16 25
#6: 2003 Facebook 19 45
为了好玩,我们还可以使用dplyr
、tidyr
和purr
获得相同的结果
library(dplyr)
library(tidyr)
library(readr)
library(purrr)
list_of_csv <- list.files(path = ".", pattern = ".csv", full.names = TRUE)
file_name <- gsub(".csv", "", basename(list_of_csv))
list_of_csv %>%
map(~ read_csv(.)) %>%
map(~ gather(data = ., key = co_name, value = value, -year)) %>%
reduce(inner_join, by = c("year", "co_name")) %>%
setNames(., c("year", "co_name", file_name))
## Source: local data frame [6 x 4]
## year co_name eps profit
## (int) (fctr) (int) (int)
## 1 2001 microsoft 12 15
## 2 2002 microsoft 15 19
## 3 2003 microsoft 16 25
## 4 2001 facebook 20 36
## 5 2002 facebook 23 40
## 6 2003 facebook 19 45
库(dplyr)
图书馆(tidyr)
图书馆(readr)
图书馆(purrr)
列表(共个)
映射(~gather(data=,key=co_name,value=value,-year))%>%
减少(内部联接,按=c(“年”,“公司名称”))%>%
集合名(,c(“年”、“公司名”、文件名))
##来源:本地数据帧[6 x 4]
##年度公司名称每股收益利润
##(国际)(fctr)(国际)(国际)
##1 2001 microsoft 12 15
##2 2002微软15 19
##3 2003 microsoft 16 25
##4 2001 facebook 20 36
##2002年5月23日40
##6 2003年11月19日45
为了好玩,我们还可以使用dplyr
、tidyr
和purrr
获得相同的结果
library(dplyr)
library(tidyr)
library(readr)
library(purrr)
list_of_csv <- list.files(path = ".", pattern = ".csv", full.names = TRUE)
file_name <- gsub(".csv", "", basename(list_of_csv))
list_of_csv %>%
map(~ read_csv(.)) %>%
map(~ gather(data = ., key = co_name, value = value, -year)) %>%
reduce(inner_join, by = c("year", "co_name")) %>%
setNames(., c("year", "co_name", file_name))
## Source: local data frame [6 x 4]
## year co_name eps profit
## (int) (fctr) (int) (int)
## 1 2001 microsoft 12 15
## 2 2002 microsoft 15 19
## 3 2003 microsoft 16 25
## 4 2001 facebook 20 36
## 5 2002 facebook 23 40
## 6 2003 facebook 19 45
库(dplyr)
图书馆(tidyr)
图书馆(readr)
图书馆(purrr)
列表(共个)
映射(~gather(data=,key=co_name,value=value,-year))%>%
减少(内部联接,按=c(“年”,“公司名称”))%>%
集合名(,c(“年”、“公司名”、文件名))
##来源:本地数据帧[6 x 4]
##年度公司名称每股收益利润
##(国际)(fctr)(国际)(国际)
##1 2001 microsoft 12 15
##2 2002微软15 19
##3 2003 microsoft 16 25
##4 2001 facebook 20 36
##2002年5月23日40
##6 2003年11月19日45
很酷。但我希望数据公司明智的ie先用于Microsoft,然后用于Facebook。@Neeraj您可以在您的代码中订购,Microsoft和Facebook的vaules正在交换。这很酷。但我希望数据公司先为Microsoft提供智能ie,然后为Facebook提供智能ie。@Neeraj您可以在您的代码中订购,Microsoft和Facebook vaules正在交换。如果您能提供一些关于使用地图的说明,那会更好,reduce
等。如果您能提供一些关于使用map
,reduce
等的说明,那就更好了。