簡體   English   中英

重塑R中數據框中的數據

[英]Reshaping Data in a dataframe in R

有沒有辦法按以下格式重新整形數據?

    Date       Student Test.1 Test.2 Test.3 
    2007/02/01   A      80      90     70  
    2007/02/01   B      90      60     90  
    2007/02/01   C      75      70     80  
    2007/02/01   D      50      80     70  

要采用以下格式?

    Date       Student Result  Test 
    2007/02/01   A      80       1   
    2007/02/01   A      90       2   
    2007/02/01   A      70       3   
    2007/02/01   B      90       1   
    2007/02/01   B      60       2   
    2007/02/01   B      90       3   
    2007/02/01   C      75       1   
    2007/02/01   C      70       2   
    2007/02/01   C      80       3   
    2007/02/01   D      50       1   
    2007/02/01   D      80       2
    2007/02/01   D      70       3      

這樣的事情會做:

reshape(x, direction='long', varying=paste('Test', 1:3, sep='.'))
          Date Student time Test id
1.1 2007/02/01       A    1   80  1
2.1 2007/02/01       B    1   90  2
3.1 2007/02/01       C    1   75  3
4.1 2007/02/01       D    1   50  4
1.2 2007/02/01       A    2   90  1
2.2 2007/02/01       B    2   60  2
3.2 2007/02/01       C    2   70  3
4.2 2007/02/01       D    2   80  4
1.3 2007/02/01       A    3   70  1
2.3 2007/02/01       B    3   90  2
3.3 2007/02/01       C    3   80  3
4.3 2007/02/01       D    3   70  4

然后,您可以根據需要重命名列。 請注意,此處的time列是您在所需輸出中標記為“ Test 這就是寬格式的列以長格式區分的方式。

melt()函數可能會有所幫助:

library(reshape)
md <- melt(df, id=c('Date','Student')

生成的“融化”數據框將是這樣的:

      Date Student variable value
2007/02/01       A    Test.1   80
2007/02/01       B    Test.1   90
2007/02/01       C    Test.1   75
2007/02/01       D    Test.1   50
2007/02/01       A    Test.1   90
...

然后,您可以重命名列和/或修改值以滿足您的需要。

然后,熔化的數據幀可以與cast()函數一起使用,以創建類似於樞軸的數據幀。 查看Quick-R教程:重塑數據

要完成常用方法的綜述,您可以查看“dplyr”+“tidyr”,它們可以像這樣一起使用:

library(dplyr)
library(tidyr)

mydf %>% gather(Time, Score, starts_with("Test"))
#          Date Student   Time Score
# 1  2007/02/01       A Test.1    80
# 2  2007/02/01       B Test.1    90
# 3  2007/02/01       C Test.1    75
# 4  2007/02/01       D Test.1    50
# 5  2007/02/01       A Test.2    90
# 6  2007/02/01       B Test.2    60
# 7  2007/02/01       C Test.2    70
# 8  2007/02/01       D Test.2    80
# 9  2007/02/01       A Test.3    70
# 10 2007/02/01       B Test.3    90
# 11 2007/02/01       C Test.3    80
# 12 2007/02/01       D Test.3    70

要獲得您正在尋找的特定表單,您可以進一步separate selectselect

mydf %>% 
  gather(Time, Score, starts_with("Test")) %>% 
  separate(Time, c("Stub", "Test")) %>% 
  select(-Stub)
#          Date Student Test Score
# 1  2007/02/01       A    1    80
# 2  2007/02/01       B    1    90
# 3  2007/02/01       C    1    75
# 4  2007/02/01       D    1    50
# 5  2007/02/01       A    2    90
# 6  2007/02/01       B    2    60
# 7  2007/02/01       C    2    70
# 8  2007/02/01       D    2    80
# 9  2007/02/01       A    3    70
# 10 2007/02/01       B    3    90
# 11 2007/02/01       C    3    80
# 12 2007/02/01       D    3    70

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM