2016-12-15 9 views
0

私は学生IDと各生徒が行った試験とのRのデータフレーム( "samp")を持っています。Rのデータフレームを名前付きリストに変更するにはどうすればいいですか?

student_id math_exam spanish_exam 
     <int>  <dbl>  <dbl> 
1   1  0   1 
2   2  1   0 
3   3  0   0 
4   4  1   1 

私が学生の1のために、それだけでスペイン語の試験が表示されます、私は学生のIDと学生ではなく、0と1を撮影している試験の名前を持つという名前のリストを作成したいと思います学生4は数学試験、スペイン語試験を表示します。

私はので、私はいくつかの基本的なテストを行なったし、私は列名をすべて1に置き換わることができるかどうかを確認置き換えるコマンドを使用してとの緊密なと思う

replace(samp, grepl(1, samp, perl=TRUE), names(samp)[2]) 

をしかし、その代わりに、私はそれをすべてを置き換えそのようなカラム名:

student_id math_exam spanish_exam 
1 math_exam math_exam math_exam 
2 math_exam math_exam math_exam 
3 math_exam math_exam math_exam 
4 math_exam math_exam math_exam 

私はちょうどSAMP $ math_examのような列を指定しても同じ結果を取得しようとしました。代わりに良いアイデアを使用していますか?あまりにも多くを求めているなら、私はまだかなり新しいRです。これに関するガイダンスは素晴らしかったです!ありがとうございました

+2

、異なる学生とその試験でリストを取得しますか? –

+0

"math_exam"と "spanish_exam"の列を要素に変換することを検討しましたか? – useR

+0

あなたは 'replace(samp $ math_exam、levels = c(0,1)、labels = c(" no_math_exam "、" math_exam ")) 、samp $ math_exam == 1、names(samp)[2]) 'これは" math_exam "を置き換えるだけです。あなたが探している出力が正確ではない。 – useR

答えて

2

ここでdata.frameをスライスし、データセットを長形式に溶かし、取得した試験のみを返します。あなたがdplyrソリューションを空想した場合

library(tidyr) 

xy <- data.frame(student_id = 1:4, math_exam = c(0, 1, 0, 1), spanish_exam = c(1, 0, 0, 1)) 

xy <- split(xy, xy$student_id) 

result <- lapply(xy, FUN = function(x) { 
    out <- gather(x, key = exam, value = taken, -student_id) 
    out[out$taken == 1, ][, -3] 
}) 

do.call(rbind, result) 

    student_id   exam 
1   1 spanish_exam 
2   2 math_exam 
4.1   4 math_exam 
4.2   4 spanish_exam 

...

library(dplyr) 

xy %>% 
    group_by(student_id) %>% 
    gather(key = exam, value = taken, -student_id) %>% 
    filter(taken == 1) %>% 
    select(-taken) 

Source: local data frame [4 x 2] 
Groups: student_id [3] 

    student_id   exam 
     <int>  <chr> 
1   2 math_exam 
2   4 math_exam 
3   1 spanish_exam 
4   4 spanish_exam 
+0

私の心にまったく同じ...あなたは勝った!私はいつも 'dplyr'を愛しています、そして、あなたはそれにもう一つの理由を与えました! –

0

私たちは、データフレームを溶かし、value == 1のためにサブセットするreshape2パッケージからmelt機能を使用することができます。 student_idに結果のデータフレームを分割する私たちは、あなたがあまりにも自分の所望の出力をしてください提供することができ、すなわち

library(reshape2) 
d3 <- melt(d1, id.vars = 'student_id') 
d3 <- d3[d3$value == 1,][-3] 
split(d3, d3$student_id) 

#$`1` 
# student_id  variable 
#5   1 spanish_exam 

#$`2` 
# student_id variable 
#2   2 math_exam 

#$`4` 
# student_id  variable 
#4   4 math_exam 
#8   4 spanish_exam 

#You can also split on variable to get a list of exams rather than a list of students, i.e. 

split(d3, d3$variable) 

#$math_exam 
# student_id variable 
# 2   2 math_exam 
# 4   4 math_exam 

#$spanish_exam 
# student_id  variable 
#5   1 spanish_exam 
#8   4 spanish_exam 

DATA

dput(d1) 
structure(list(student_id = 1:4, math_exam = c(0L, 1L, 0L, 1L 
), spanish_exam = c(1L, 0L, 0L, 1L)), .Names = c("student_id", 
"math_exam", "spanish_exam"), class = "data.frame", row.names = c("1", 
"2", "3", "4")) 
+0

huh?それはどうですか@ManojKumar?検査列以外は整数ではなく数値ですか? – Sotos

+0

@ManojKumarは詳しく説明しますか? – Sotos

+0

私は間違っていません。私はちょうどあなたが意味することを理解しようとしています。 – Sotos

関連する問題