[英]Copy two column values from one data frame to one column but two rows in another data frame
I'm new to R and started two weeks ago. 我是R的新手,于两周前开始。 I have a data set which i'm trying to convert into a panel data. 我有一个数据集,我正在尝试将其转换为面板数据。 See data set below 查看以下数据集
ID Empl93 Empl95 Sales93 Sales95 1 20 30 200 150 2 14 40 350 90 4 50 10 100 220 9 29 45 400 560 20 42 23 190 350
I need to convert this to a panel as below 我需要将其转换为如下面板
ID Emply Sales Year 1 20 200 1993 1 30 150 1995 2 14 350 1993 2 40 90 1995 4 50 100 1993 4 10 220 1995 9 29 400 1993 9 45 560 1995
The rows are about 1600 and made up of random IDs, I can add the new column "Year" to the data frame. 这些行大约有1600个,由随机ID组成,我可以将新列“ Year”添加到数据框中。 I have also been able to add the duplicate rows using the code below: 我还能够使用以下代码添加重复的行:
newdata <- newdata[rep(seq_len(nrow(newdata)), each=2),]
My problem is how to copy the Empl93, Empl95, Sales93, Sales95 values from first data frame and paste in the correspondent years in the panel. 我的问题是如何从第一个数据框中复制Empl93,Empl95,Sales93,Sales95值并粘贴到面板中的相应年份。 Thank you. 谢谢。
A solution using dplyr
and tidyr
. 使用dplyr
和tidyr
解决方案。
library(dplyr)
library(tidyr)
dt2 <- dt %>%
gather(Key, Value, -ID) %>%
extract(Key, into = c("Type", "Year"), "([A-Za-z]+)([0-9]+)") %>%
mutate(Type = sub("Empl", "Emply", Type),
Year = as.integer(paste0("19", Year))) %>%
spread(Type, Value) %>%
select(ID, Emply, Sales, Year)
dt2
ID Emply Sales Year
1 1 20 200 1993
2 1 30 150 1995
3 2 14 350 1993
4 2 40 90 1995
5 4 50 100 1993
6 4 10 220 1995
7 9 29 400 1993
8 9 45 560 1995
9 20 42 190 1993
10 20 23 350 1995
DATA 数据
dt <- read.table(text = "ID Empl93 Empl95 Sales93 Sales95
1 20 30 200 150
2 14 40 350 90
4 50 10 100 220
9 29 45 400 560
20 42 23 190 350",
header = TRUE, stringsAsFactors = FALSE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.