将数据集从广泛更改为长,同时保留组ID,并收集列

时间:2017-02-25 02:00:46

标签: r

我真的很感激帮助将这些杂乱的新调查数据集合成一个可用的形式。它以一种奇怪的方式收集,现在我有奇怪的数据可以使用。我看过tidyr,并使用这些方法无止境。我怀疑我的问题是我正在考虑这个数据集都错了,我对一些真正的答案视而不见。但考虑到我需要对这个df做的所有事情,我无法弄清楚从哪里开始,从而从哪里开始谷歌搜索。

我需要什么: 为每个人做自己的行 每个人都保留其GroupID和Treated价值 对于当前附加到每个人的变量,成为列(年龄,体重,身高)

假(并且小得多):

structure(list(GroupID = 1:5, Treated = c("Y", "Y", "N", "Y", 
"N"), person1_age = c(45L, 33L, 71L, 19L, 52L), person1_weight = c(187L, 
145L, 136L, 201L, 168L), person1_height = c(69L, 64L, 51L, 70L, 
66L), person2_age = c(54L, 20L, 48L, 63L, 26L), person2_weight = c(140L, 
122L, 186L, 160L, 232L), person2_height = c(62L, 70L, 65L, 72L, 
74L), person3_age = c(21L, 56L, 40L, 59L, 67L), person3_weight = c(112L, 
143L, 187L, 194L, 159L), person3_height = c(61L, 69L, 73L, 63L, 
72L)), .Names = c("GroupID", "Treated", "person1_age", "person1_weight", 
"person1_height", "person2_age", "person2_weight", "person2_height", 
"person3_age", "person3_weight", "person3_height"), row.names = c(NA, 
5L), class = "data.frame")

非常感谢您指出的任何帮助或进一步阅读。

2 个答案:

答案 0 :(得分:1)

reshape可以使用适当的参数执行此操作:

> reshape(x, direction="long", varying=names(x)[3:11], timevar='person', v.names=c('height', 'age', 'weight'), sep='_')
    GroupID Treated person height age weight id
1.1       1       Y      1    187  45     69  1
2.1       2       Y      1    145  33     64  2
3.1       3       N      1    136  71     51  3
4.1       4       Y      1    201  19     70  4
5.1       5       N      1    168  52     66  5
1.2       1       Y      2    140  54     62  1
2.2       2       Y      2    122  20     70  2
3.2       3       N      2    186  48     65  3
4.2       4       Y      2    160  63     72  4
5.2       5       N      2    232  26     74  5
1.3       1       Y      3    112  21     61  1
2.3       2       Y      3    143  56     69  2
3.3       3       N      3    187  40     73  3
4.3       4       Y      3    194  59     63  4
5.3       5       N      3    159  67     72  5

这取决于原始数据中列的顺序,varying参数,在原始数据中按升序排列。

如果不是这种情况,请手动指定varying。以下是上面使用的内容:

> names(x)[3:11]
[1] "person1_age"    "person1_weight" "person1_height" "person2_age"    "person2_weight" "person2_height"
[7] "person3_age"    "person3_weight" "person3_height"

答案 1 :(得分:0)

我们还可以使用melt中的data.table patternsmeasure参数中可以使用多个library(data.table) melt(setDT(x), measure = patterns("age$", "weight$", "height$"), variable.name = "person", value.name = c("age", "weight", "height")) # GroupID Treated person age weight height # 1: 1 Y 1 45 187 69 # 2: 2 Y 1 33 145 64 # 3: 3 N 1 71 136 51 # 4: 4 Y 1 19 201 70 # 5: 5 N 1 52 168 66 # 6: 1 Y 2 54 140 62 # 7: 2 Y 2 20 122 70 # 8: 3 N 2 48 186 65 # 9: 4 Y 2 63 160 72 #10: 5 N 2 26 232 74 #11: 1 Y 3 21 112 61 #12: 2 Y 3 56 143 69 #13: 3 N 3 40 187 73 #14: 4 Y 3 59 194 63 #15: 5 N 3 67 159 72

 //paddle node and its physicsbody
    paddle = childNode(withName: "paddle") as! SKSpriteNode
    paddle.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: paddle.size.width,
                                                                         height: paddle.size.height))
    paddle.physicsBody?.isDynamic = false
    paddle.physicsBody?.affectedByGravity = false;
    paddle.physicsBody?.friction = 100
    paddle.physicsBody?.restitution = 0
    //paddle.physicsBody?.linearDamping = 1
    //paddle.physicsBody?.angularDamping = 1

//Second node
rect.position = CGPoint(x: 100, y:500)
        rect.fillColor = UIColor(hue: 215/360, saturation: 49/100, brightness: 54/100, alpha: 1.0)
        rect.strokeColor = UIColor(hue: 215/360, saturation: 49/100, brightness: 54/100, alpha: 1.0)
        rect.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: rect.frame.width, height: rect.frame.height))
        rect.physicsBody?.friction = 0.4
        rect.physicsBody?.restitution = 0
        rect.physicsBody?.mass = 100
        rect.physicsBody?.allowsRotation = true
        rect.physicsBody?.isDynamic = true
        rect.physicsBody?.linearDamping = 0
        rect.physicsBody?.angularDamping = 0
        //rect.physicsBody?.velocity = CGVector(dx: 0, dy: -700)
        rect.physicsBody!.applyImpulse(CGVector(dx: 2.0, dy: -2.0))
        rect.lineWidth = 10
        addChild(rect)