Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中重塑数据帧中的数据_R_Reshape - Fatal编程技术网

在R中重塑数据帧中的数据

在R中重塑数据帧中的数据,r,reshape,R,Reshape,是否有任何方法可以按以下格式重新塑造数据 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 Dat

是否有任何方法可以按以下格式重新塑造数据

    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      
是否转换为以下格式

    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')
然后可以重命名列和/或修改值以满足需要


然后,熔化的数据帧可以与
cast()
函数一起使用,以创建类似枢轴的数据帧。检查。

要完成常见方法的汇总,您可以查看“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
select
进一步执行两个步骤:

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