我希望有人可以帮助我在这里找出我的数据有什么问题。我试图将几个随时间变化的协变量从长变为宽,如下所示:
dummy.data<-data.frame(ID=rep('ID1',10),var1=rep('A',10),age=seq(1,10,1),
var2=c(1,1,0,0,0,0,1,1,0,0),var3=seq(5,14,1))
dummy.data%>%gather(age.var,value,-ID,-var1,-age)%>%
unite(age.var,age.var,age)%>%
spread(age.var,value)
但是当我使用一个真实的数据集时,一些观察结果导致将每个时变协变量写入不同的行,将另一个变量填充为NA,我不知道为什么:
library(tidyverse)
real.data= structure(list(NestID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("SwanA_17",
"SwanC_17"), class = "factor"), detection = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
), pred.fish = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), WA = c(208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728, 208.8195728, 208.8195728, 208.8195728, 208.8195728,
208.8195728), year = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), cdoy = c(-10, -9, -8,
-7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, -35, -34,
-33, -32, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15,
-14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1,
0, 1, 2, 3, 4), age = c("0", "1", "2", "3", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
"35", "36", "37", "38", "39", "0", "1", "2", "3", "10", "11",
"12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
"34", "35", "36", "37", "38", "39"), known = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
), unk = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("NestID", "detection",
"pred.fish", "WA", "year", "cdoy", "age", "known", "unk"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -68L))
gather(real.data,variable,value,c(detection,cdoy))%>%
mutate(age=ifelse(nchar(age)<2,paste0('0',age),age))%>%
unite(temp.var,variable,age)%>%
spread(temp.var,value)
答案 0 :(得分:0)
由于某种原因,spread或dcast可以处理作为公式向量的ID /左侧的数字向量。首先将WA转换为浮点数以外的其他值(例如,字符,整数)会产生所需的输出。