简体   繁体   English

将数据框变量重塑为列表

[英]Reshape Data frame variables into list

I'm havin an issue with R. 我对R有疑问。

I have the following dataframe: 我有以下数据框:

FirstName LastName Exercice1 Exercice2
   Eric       A        15        12
   Eric       A        14        14
   Eric       A        12        15
   Paul       B        12        14
   Paul       B        14        14
   Joe        C        15        12
   Joe        C        15        17
   Joe        C        16        17
   Joe        C        18        19

And I want to change it into the following: 我想将其更改为以下内容:

FirstName   LastName            Mark
                         Exercice1 Exercice2
   Eric         A            15        12
                             14        14
                             12        15
                         Exercice1 Exercice2
   Paul        B             12        14
                             14        14
                         Exercice1 Exercice2
   Joe        C              15        12
                             15        17
                             16        17
                             18        19

In short I would like to group for each students their marks for every test into a single variable that is a data frame. 简而言之,我想为每位学生将每项考试的分数归为一个数据框变量。

Do you have any ideas if that is possible and how I should do? 如果有可能,您有什么想法,我应该怎么做?

If all you need is the Mark column to be a data.frame of the student, then you can use this: 如果您只需要Mark列作为学生的data.frame,则可以使用此列:

dat <- read.table(text = "FirstName LastName Exercice1 Exercice2
   Eric       A        15        12
                  Eric       A        14        14
                  Eric       A        12        15
                  Paul       B        12        14
                  Paul       B        14        14
                  Joe        C        15        12
                  Joe        C        15        17
                  Joe        C        16        17
                  Joe        C        18        19", stringsAsFactors = FALSE, header = TRUE)

dat2 <- dat[!duplicated(dat[,1:2]),1:2]
dat2$Mark <- I(split(dat[,3:4], list(dat$FirstName, dat$LastName), drop = TRUE))

dat2
#  FirstName LastName         Mark
#1      Eric        A c(15, 14....
#4      Paul        B c(12, 14....
#6       Joe        C c(15, 15....

Mark is a list of dataframes: 标记是数据框的列表:

> dat2$Mark
$Eric.A
  Exercice1 Exercice2
1        15        12
2        14        14
3        12        15

$Paul.B
  Exercice1 Exercice2
4        12        14
5        14        14

$Joe.C
  Exercice1 Exercice2
6        15        12
7        15        17
8        16        17
9        18        19

Now in order to print like you showed you'd need a custom print function, but you shouldn't need that anyway. 现在,要像显示的那样进行打印,您需要自定义打印功能,但是无论如何您都不需要。 Or just add empty rows to get your desired print output. 或者只是添加空行以获得所需的打印输出。 This isn't pretty nor like your output, but it's a start: 这不是很好,也不像您的输出,但这是一个开始:

dat3 <- dat
dat3[duplicated(dat[,1:2]),1:2] <- ""
print(dat3, row.names = FALSE)
# FirstName LastName Exercice1 Exercice2
#      Eric        A        15        12
#                           14        14
#                           12        15
#      Paul        B        12        14
#                           14        14
#       Joe        C        15        12
#                           15        17
#                           16        17
#                           18        19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM