R 如何更改数据帧的格式

R 如何更改数据帧的格式,r,dataframe,R,Dataframe,我有以下格式的数据帧: > example_df P_ID Income2000 Income2001 Income2002 1 1 21 22 23 2 2 15 15 15 3 3 10 13 15 其中,我有多个收入超过三年的人,分别为2000年、2001年和2002年 现在我想知道是否有一种快速有效的方法可以将上述数

我有以下格式的数据帧:

> example_df
  P_ID Income2000 Income2001 Income2002
1    1         21         22         23
2    2         15         15         15
3    3         10         13         15
其中,我有多个收入超过三年的人,分别为2000年、2001年和2002年

现在我想知道是否有一种快速有效的方法可以将上述数据帧转换为如下所示的数据帧:

> example_df2
  Year P_ID Income
1 2000    1     21
2 2001    1     22
3 2002    1     23
4 2000    2     15
5 2001    2     15
6 2002    2     15
7 2000    3     10
8 2001    3     13
9 2002    3     15

我们可以使用
pivot\u longer
将“宽”改为“长”,然后从列名中删除子字符串

library(dplyr)
library(tidyr)
example_df %>% 
    pivot_longer(cols = -P_ID, names_to = 'Year',values_to = 'Income') %>%
    mutate(Year = readr::parse_number(Year))
# A tibble: 9 x 3
#   P_ID  Year Income
#  <int> <dbl>  <int>
#1     1  2000     21
#2     1  2001     22
#3     1  2002     23
#4     2  2000     15
#5     2  2001     15
#6     2  2002     15
#7     3  2000     10
#8     3  2001     13
#9     3  2002     15
库(dplyr)
图书馆(tidyr)
示例_df%>%
pivot_longer(cols=-P_ID,name_to='Year',values_to='Income')%>%
变异(年份=读取器::解析_编号(年份))
#一个tibble:9x3
#P_ID年收入
#     
#1     1  2000     21
#2     1  2001     22
#3     1  2002     23
#4     2  2000     15
#5     2  2001     15
#6     2  2002     15
#7     3  2000     10
#8     3  2001     13
#9     3  2002     15
数据
example\u df我们可以使用
pivot\u longer
将“宽”改为“长”,然后从列名中删除子字符串

library(dplyr)
library(tidyr)
example_df %>% 
    pivot_longer(cols = -P_ID, names_to = 'Year',values_to = 'Income') %>%
    mutate(Year = readr::parse_number(Year))
# A tibble: 9 x 3
#   P_ID  Year Income
#  <int> <dbl>  <int>
#1     1  2000     21
#2     1  2001     22
#3     1  2002     23
#4     2  2000     15
#5     2  2001     15
#6     2  2002     15
#7     3  2000     10
#8     3  2001     13
#9     3  2002     15
库(dplyr)
图书馆(tidyr)
示例_df%>%
pivot_longer(cols=-P_ID,name_to='Year',values_to='Income')%>%
变异(年份=读取器::解析_编号(年份))
#一个tibble:9x3
#P_ID年收入
#     
#1     1  2000     21
#2     1  2001     22
#3     1  2002     23
#4     2  2000     15
#5     2  2001     15
#6     2  2002     15
#7     3  2000     10
#8     3  2001     13
#9     3  2002     15
数据
示例\u df这里是一个基本的R方法

reshape(example_df, 
        direction = "long",
        idvar = "P_ID",
        varying=list(names(example_df)[2:4]),
        timevar="Year",
        times=c(2000,2001,2002),
        v.names = "Income")

       P_ID Year Income2000
1.2000    1 2000         21
2.2000    2 2000         15
3.2000    3 2000         10
1.2001    1 2001         22
2.2001    2 2001         15
3.2001    3 2001         13
1.2002    1 2002         23
2.2002    2 2002         15
3.2002    3 2002         15

下面是一个基本的R方法

reshape(example_df, 
        direction = "long",
        idvar = "P_ID",
        varying=list(names(example_df)[2:4]),
        timevar="Year",
        times=c(2000,2001,2002),
        v.names = "Income")

       P_ID Year Income2000
1.2000    1 2000         21
2.2000    2 2000         15
3.2000    3 2000         10
1.2001    1 2001         22
2.2001    2 2001         15
3.2001    3 2001         13
1.2002    1 2002         23
2.2002    2 2002         15
3.2002    3 2002         15

你好,阿克伦,我不知道为什么这篇文章被否决了。我干得很好!非常感谢:)你好,阿克伦,我不知道为什么这篇文章被否决了。我干得很好!非常感谢:)@akrun,我没有对你的答案投反对票(我投反对票是为了补偿),作为你,我不明白为什么。我同意复制链接不能完全概括OP的预期输出,但它是解决方案的主要部分。@akrun,我没有对你的答案投反对票(我投了反对票以进行补偿),作为你,我不明白为什么。我同意复制链接不能完全概括OP的预期输出,但它是解决方案的主要部分。