简体   繁体   English

将两列值从一个数据帧复制到一列,但在另一数据帧中复制两行

[英]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 . 使用dplyrtidyr解决方案。

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.

相关问题 如果前两列都匹配,则将数据框的一列中的值添加到另一数据框的新列中 - adding values from one column of a data frame into a new column of another dataframe if the first two columns in both match R:从一个数据框中提取行,基于列名匹配另一个数据框中的值 - R: Extract Rows from One Data Frame, Based on Column Names Matching Values from Another Data Frame 根据一列的条件获取数据框中两行的平均值 - Getting the mean of two rows in data frame based on the condition of one column 如果 R 的 data.frame 的一列中存在两个指定值,如何保留一组的所有行 - How to keep all rows of one group if two specified values are present in one column in data.frame in R 根据索引将列从一个 data.frame 复制到另一个 - Copy column from one data.frame to another based on index 根据一列值将 data.frame 一分为二 - split data.frame in two based on one column values 根据行数不同的另一个数据框的值将值分配给一个数据框的列 - Assign values to a column of one data frame based on values of another data frame with different number of rows 使用r中另一数据帧中的两列对一个数据帧中的列进行子集 - Subseting column in one data frame using two columns in another data frame in r 如何将一个数据框中的特定行的总和输出到另一个数据框中的新列? - How to output the sum of specific rows from one data frame to a new column in another data frame? 从一个数据框中选择所有列值都存在于第二个数据框中的行 - Select rows from one data frame where all column values exist in the second data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM