将数据文件从宽格式转换为长格式,以适应R中的顺序混合模型

将数据文件从宽格式转换为长格式,以适应R中的顺序混合模型,r,reshape,data-conversion,reshape2,ordinal,R,Reshape,Data Conversion,Reshape2,Ordinal,我正在处理一个宽格式的数据集,如 > data=read.csv("http://www.kuleuven.be/bio/ento/temp/data.csv") > data factor1 factor2 count_1 count_2 count_3 1 a a 1 2 0 2 a b 3 0 0 3 b a 1

我正在处理一个宽格式的数据集,如

> data=read.csv("http://www.kuleuven.be/bio/ento/temp/data.csv")
> data
  factor1 factor2 count_1 count_2 count_3
1       a       a       1       2       0
2       a       b       3       0       0
3       b       a       1       2       3
4       b       b       2       2       0
5       c       a       3       4       0
6       c       b       1       1       0
其中,factor1和factor2是不同的因素,我想考虑(事实上,我有2个以上的因素,但这并不重要),count_1到count_3是按顺序进行的攻击性互动计数(3>2>1)。我现在想把这个数据集转换成长格式

   factor1 factor2 aggression
1        a       a          1
2        a       a          2
3        a       a          2
4        a       b          1
5        a       b          1
6        a       b          1
7        b       a          1
8        b       a          2
9        b       a          2
10       b       a          3
11       b       a          3
12       b       a          3
13       b       b          1
14       b       b          1
15       b       b          2
16       b       b          2
17       c       a          1
18       c       a          1
19       c       a          1
20       c       a          2
21       c       a          2
22       c       a          2
23       c       a          2
24       c       b          1
25       c       b          2
是否有人知道如何在不使用for…to循环的情况下执行此操作,例如使用package
reforme2
?(我意识到它应该使用
melt
,但我只是还没有找到正确的语法)

编辑:对于那些碰巧也需要这种功能的人,下面是阿南达的答案,它包含在一个小功能中:

    widetolong.ordinal<-function(data,factors,responses,responsename) {
    library(reshape2)
    data$ID=1:nrow(data) # add an ID to preserve row order
    dL=melt(data, id.vars=c("ID", factors)) # `melt` the data
    dL=dL[order(dL$ID), ] # sort the molten data
    dL[,responsename]=match(dL$variable,responses) # convert reponses to ordinal scores
    dL[,responsename]=factor(dL[,responsename],ordered=T)
    dL=dL[dL$value != 0, ] # drop rows where `value == 0`
    out=dL[rep(rownames(dL), dL$value), c(factors, responsename)] # use `rep` to "expand" `data.frame` & drop unwanted columns
    rownames(out) <- NULL
    return(out)
    }

    # example
    data <- read.csv("http://www.kuleuven.be/bio/ento/temp/data.csv")
    widetolong.ordinal(data,c("factor1","factor2"),c("count_1","count_2","count_3"),"aggression")
“重塑2”中的widetolong.ordinal
melt
只会帮你解决部分问题。要完成剩下的步骤,只需使用base R中的
rep

data <- read.csv("http://www.kuleuven.be/bio/ento/temp/data.csv")
library(reshape2)

## Add an ID if the row order is importantt o you
data$ID <- 1:nrow(data)

## `melt` the data
dL <- melt(data, id.vars=c("ID", "factor1", "factor2"))

## Sort the molten data, if necessary
dL <- dL[order(dL$ID), ]

## Extract the numeric portion of the "variable" variable
dL$aggression <- gsub("count_", "", dL$variable)

## Drop rows where `value == 0`
dL <- dL[dL$value != 0, ]

## Use `rep` to "expand" your `data.frame`.
## Drop any unwanted columns at this point.
out <- dL[rep(rownames(dL), dL$value), c("factor1", "factor2", "aggression")]

查看
重塑2
包装中的
melt
。通过简单的示例,我相信您将能够在自己的数据中识别相应的
id.vars
measure.vars
。我认为我使用SPSS的唯一目的是,就是要做到这一点。@AnandaMahto:很抱歉我的例子太长了——我现在编辑了它,给出了一个更抽象、更简单的例子——感谢Henrik的指点——我意识到熔化可能是一种方式,但是仍然没有找到正确的语法,尽管出于我的目的…如果你向我们展示你的尝试,你更有可能得到帮助。请再看一次。数据与您的数据非常相似:两个因素(受试者性别与因素1因素2)和三个测量值(控制条件1条件2与计数\u 1计数\u 2计数\u 3)。@Henrik,这是一个类似的问题,但这个问题需要的不仅仅是
melt
out
#      factor1 factor2 aggression
# 1          a       a          1
# 7          a       a          2
# 7.1        a       a          2
# 2          a       b          1
# 2.1        a       b          1
# 2.2        a       b          1
# 3          b       a          1
# 9          b       a          2
# 9.1        b       a          2
# 15         b       a          3
# 15.1       b       a          3
# 15.2       b       a          3
# 4          b       b          1
# 4.1        b       b          1
# 10         b       b          2
# 10.1       b       b          2
# 5          c       a          1
# 5.1        c       a          1
# 5.2        c       a          1
# 11         c       a          2
# 11.1       c       a          2
# 11.2       c       a          2
# 11.3       c       a          2
# 6          c       b          1
# 12         c       b          2