如何从调查数据中分离出非零答案

时间:2018-08-22 17:53:54

标签: r dataframe survey

我有调查数据,其中在6个不同的时期内向同一个人询问相同的问题。有时他们回答(在这种情况下,我们的分数是1到10),有时他们没有回答(在这种情况下,答案是0)。

最后,我得到了一个看起来像这样的数据帧(唯一的区别是,在此示例中,答案是从1到2,这仅仅是因为以这种方式生成足够数量的0更容易了) ):

period_1 <- sample(0:2, 100, replace=T)
period_2 <- sample(0:2, 100, replace=T)
period_3 <- sample(0:2, 100, replace=T)
period_4 <- sample(0:2, 100, replace=T)
period_5 <- sample(0:2, 100, replace=T)
period_6 <- sample(0:2, 100, replace=T)

df <- cbind(period_1, period_2, period_3, period_4, period_5, period_6)
head(df)

     period_1 period_2 period_3 period_4 period_5 period_6
[1,]        0        2        1        1        0        1
[2,]        2        1        1        2        0        0
[3,]        1        0        2        0        1        1
[4,]        1        2        2        1        0        2
[5,]        1        1        2        2        0        2
[6,]        1        0        1        2        2        0

现在,我想看看他们的答案随着时间的变化。但是对于当前的数据帧结构,这有点尴尬:例如,我不能仅将时间段1与时间段2进行比较,因为它们并没有在时间段1(或2)全部回答。 取而代之的是,我想要的是一个数据帧,该数据帧将在一个向量中显示其第一个答案,无论该答案来自哪个时期,然后是第二个答案,依此类推……

换句话说,在Survey_1中获得第一个非0答案,在Survey_2中获得第二个非0答案,依此类推… 这可能不是最好的解决方案,但它是最简单的解决方案,对我来说也很好。

这将允许我打开它:

     period_1 period_2 period_3 period_4 period_5 period_6
[1,]        0        2        1        1        0        1
[2,]        2        1        1        2        1        0
[3,]        1        0        2        0        1        1

对此:

     survey_1 survey_2 survey_3 survey_4 survey_5 survey_6
[1,]        2        1        1        1        0        0
[2,]        2        1        1        2        1        0
[3,]        1        2        1        1        0        0

但是,老实说,我仍然是R和编程领域的新手,我什至不知道从哪里开始实现这一目标,而且我已经坚持了一段时间,没有做任何事情。寻求解决方案。

请问有人可以向我提供提示,甚至提供示例代码,让我获得所需的结果吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

我们可以根据行的元素是否为0来使用applyorder

df[] <- t(apply(df, 1, function(x) x[order(x == 0)]))

结果:

     period_1 period_2 period_3 period_4 period_5 period_6
[1,]        1        2        2        1        0        0
[2,]        2        2        1        0        0        0
[3,]        1        1        1        2        2        0
[4,]        2        2        1        2        1        0
[5,]        2        1        1        1        1        1
[6,]        2        2        1        1        0        0

数据:

df <- structure(c(0L, 2L, 1L, 2L, 2L, 0L, 1L, 0L, 1L, 2L, 1L, 2L, 0L, 
2L, 1L, 1L, 1L, 2L, 2L, 0L, 2L, 2L, 1L, 1L, 2L, 0L, 2L, 1L, 1L, 
1L, 1L, 1L, 0L, 0L, 1L, 0L), .Dim = c(6L, 6L), .Dimnames = list(
    NULL, c("period_1", "period_2", "period_3", "period_4", "period_5", 
    "period_6")))
相关问题