R 使用时不变变量上的某些缺失数据(NA';s)从长到宽进行重塑
使用R 使用时不变变量上的某些缺失数据(NA';s)从长到宽进行重塑,r,reshape,R,Reshape,使用stats:::restrape()from base将数据从长格式转换为宽格式时,对于指定为时间不变的任何变量,restrape只取第一个观察值,如果变量实际以某种方式变化,则输出警告。在我的例子中,我缺少我想指定为时间不变的变量的数据,但由于我在其他时间点有这些数据,我希望使用这些时间点的值,而不是第一次观察到的NA testdata <- data.frame(matrix(c(1,1,2,3,4,3.5,NA,6,4,1,2,1), nrow = 3)) colnames(te
stats:::restrape()
from base将数据从长格式转换为宽格式时,对于指定为时间不变的任何变量,restrape
只取第一个观察值,如果变量实际以某种方式变化,则输出警告。在我的例子中,我缺少我想指定为时间不变的变量的数据,但由于我在其他时间点有这些数据,我希望使用这些时间点的值,而不是第一次观察到的NA
testdata <- data.frame(matrix(c(1,1,2,3,4,3.5,NA,6,4,1,2,1), nrow = 3))
colnames(testdata) <- c("id", "process1", "timeinvariant", "time")
# > testdata
# id process1 timeinvariant time
# 1 1 3.0 NA 1
# 2 1 4.0 6 2
# 3 2 3.5 4 1
# Note here the missing data on the invariant process at time 1
reshaped <- reshape(testdata, v.names="process1", direction = "wide")
# > reshaped
# id timeinvariant process1.1 process1.2
# 1 1 NA 3.0 4
# 3 2 4 3.5 NA
testdata我不知道如何修复该问题,但修复症状的一种方法是按顺序向下推NA值
testdata <- testdata[order(testdata$timeinvariant),]
testdata
# id process1 timeinvariant time
#3 2 3.5 4 1
#2 1 4.0 6 2
#1 1 3.0 NA 1
reshaped<-reshape(testdata,v.names="process1",direction="wide")
reshaped
# id timeinvariant process1.1 process1.2
#3 2 4 3.5 NA
#2 1 6 3.0 4
testdata我不知道如何修复该问题,但修复症状的一种方法是按顺序向下推NA值
testdata <- testdata[order(testdata$timeinvariant),]
testdata
# id process1 timeinvariant time
#3 2 3.5 4 1
#2 1 4.0 6 2
#1 1 3.0 NA 1
reshaped<-reshape(testdata,v.names="process1",direction="wide")
reshaped
# id timeinvariant process1.1 process1.2
#3 2 4 3.5 NA
#2 1 6 3.0 4
testdata我不知道如何修复该问题,但修复症状的一种方法是按顺序向下推NA值
testdata <- testdata[order(testdata$timeinvariant),]
testdata
# id process1 timeinvariant time
#3 2 3.5 4 1
#2 1 4.0 6 2
#1 1 3.0 NA 1
reshaped<-reshape(testdata,v.names="process1",direction="wide")
reshaped
# id timeinvariant process1.1 process1.2
#3 2 4 3.5 NA
#2 1 6 3.0 4
testdata我不知道如何修复该问题,但修复症状的一种方法是按顺序向下推NA值
testdata <- testdata[order(testdata$timeinvariant),]
testdata
# id process1 timeinvariant time
#3 2 3.5 4 1
#2 1 4.0 6 2
#1 1 3.0 NA 1
reshaped<-reshape(testdata,v.names="process1",direction="wide")
reshaped
# id timeinvariant process1.1 process1.2
#3 2 4 3.5 NA
#2 1 6 3.0 4
testdata如果每个id
始终至少有一个timeinvariant
的非缺失值,并且每个id
的所有timeinvariant
的(非缺失)值都是相同的(因为它是时不变的),您不能创建一个新列来填充时间不变
中的NA
值,然后使用该列重新塑造形状吗?例如:
# Add another row to your data frame so that we'll have 2 NA values to deal with
td <- data.frame(matrix(c(1,1,2,1,3,4,3.5,4.5,NA,6,4,NA,1,2,1,3), nrow = 4))
colnames(td) <- c("id", "process1", "timeinvariant", "time")
# Create new column timeinvariant2, which fills in NAs from timeinvariant,
# then reshape using that column
library(dplyr)
td.wide = td %>%
group_by(id) %>%
mutate(timeinvariant2=max(timeinvariant, na.rm=TRUE)) %>%
dcast(id + timeinvariant2 ~ time, value.var='process1')
# Paste "process1." onto names of all "time" columns
names(td.wide) = gsub("(^[0-9]*$)", "process1\\.\\1", names(td.wide) )
td.wide
id timeinvariant2 process1.1 process1.2 process1.3
1 1 6 3.0 4 4.5
2 2 4 3.5 NA NA
#在数据框中添加另一行,这样我们将有2个NA值需要处理
td%
mutate(timeinvariant2=max(timeinvariant,na.rm=TRUE))%>%
dcast(id+timeinvariant2~time,value.var='process1')
#将“process1.”粘贴到所有“time”列的名称上
名称(td.wide)=gsub((^[0-9]*$),“process1\\.\\1”,名称(td.wide))
td.wide
id时间不变量2进程1.1进程1.2进程1.3
1 1 6 3.0 4 4.5
2 2 4 3.5不适用
如果每个id
始终至少有一个时不变的非缺失值,并且每个id
的所有时不变的(非缺失)值都是相同的(因为它是时不变的),您不能创建一个新列来填充时间不变
中的NA
值,然后使用该列重新塑造形状吗?例如:
# Add another row to your data frame so that we'll have 2 NA values to deal with
td <- data.frame(matrix(c(1,1,2,1,3,4,3.5,4.5,NA,6,4,NA,1,2,1,3), nrow = 4))
colnames(td) <- c("id", "process1", "timeinvariant", "time")
# Create new column timeinvariant2, which fills in NAs from timeinvariant,
# then reshape using that column
library(dplyr)
td.wide = td %>%
group_by(id) %>%
mutate(timeinvariant2=max(timeinvariant, na.rm=TRUE)) %>%
dcast(id + timeinvariant2 ~ time, value.var='process1')
# Paste "process1." onto names of all "time" columns
names(td.wide) = gsub("(^[0-9]*$)", "process1\\.\\1", names(td.wide) )
td.wide
id timeinvariant2 process1.1 process1.2 process1.3
1 1 6 3.0 4 4.5
2 2 4 3.5 NA NA
#在数据框中添加另一行,这样我们将有2个NA值需要处理
td%
mutate(timeinvariant2=max(timeinvariant,na.rm=TRUE))%>%
dcast(id+timeinvariant2~time,value.var='process1')
#将“process1.”粘贴到所有“time”列的名称上
名称(td.wide)=gsub((^[0-9]*$),“process1\\.\\1”,名称(td.wide))
td.wide
id时间不变量2进程1.1进程1.2进程1.3
1 1 6 3.0 4 4.5
2 2 4 3.5不适用
如果每个id
始终至少有一个时不变的非缺失值,并且每个id
的所有时不变的(非缺失)值都是相同的(因为它是时不变的),您不能创建一个新列来填充时间不变
中的NA
值,然后使用该列重新塑造形状吗?例如:
# Add another row to your data frame so that we'll have 2 NA values to deal with
td <- data.frame(matrix(c(1,1,2,1,3,4,3.5,4.5,NA,6,4,NA,1,2,1,3), nrow = 4))
colnames(td) <- c("id", "process1", "timeinvariant", "time")
# Create new column timeinvariant2, which fills in NAs from timeinvariant,
# then reshape using that column
library(dplyr)
td.wide = td %>%
group_by(id) %>%
mutate(timeinvariant2=max(timeinvariant, na.rm=TRUE)) %>%
dcast(id + timeinvariant2 ~ time, value.var='process1')
# Paste "process1." onto names of all "time" columns
names(td.wide) = gsub("(^[0-9]*$)", "process1\\.\\1", names(td.wide) )
td.wide
id timeinvariant2 process1.1 process1.2 process1.3
1 1 6 3.0 4 4.5
2 2 4 3.5 NA NA
#在数据框中添加另一行,这样我们将有2个NA值需要处理
td%
mutate(timeinvariant2=max(timeinvariant,na.rm=TRUE))%>%
dcast(id+timeinvariant2~time,value.var='process1')
#将“process1.”粘贴到所有“time”列的名称上
名称(td.wide)=gsub((^[0-9]*$),“process1\\.\\1”,名称(td.wide))
td.wide
id时间不变量2进程1.1进程1.2进程1.3
1 1 6 3.0 4 4.5
2 2 4 3.5不适用
如果每个id
始终至少有一个时不变的非缺失值,并且每个id
的所有时不变的(非缺失)值都是相同的(因为它是时不变的),您不能创建一个新列来填充时间不变
中的NA
值,然后使用该列重新塑造形状吗?例如:
# Add another row to your data frame so that we'll have 2 NA values to deal with
td <- data.frame(matrix(c(1,1,2,1,3,4,3.5,4.5,NA,6,4,NA,1,2,1,3), nrow = 4))
colnames(td) <- c("id", "process1", "timeinvariant", "time")
# Create new column timeinvariant2, which fills in NAs from timeinvariant,
# then reshape using that column
library(dplyr)
td.wide = td %>%
group_by(id) %>%
mutate(timeinvariant2=max(timeinvariant, na.rm=TRUE)) %>%
dcast(id + timeinvariant2 ~ time, value.var='process1')
# Paste "process1." onto names of all "time" columns
names(td.wide) = gsub("(^[0-9]*$)", "process1\\.\\1", names(td.wide) )
td.wide
id timeinvariant2 process1.1 process1.2 process1.3
1 1 6 3.0 4 4.5
2 2 4 3.5 NA NA
#在数据框中添加另一行,这样我们将有2个NA值需要处理
td%
mutate(timeinvariant2=max(timeinvariant,na.rm=TRUE))%>%
dcast(id+timeinvariant2~time,value.var='process1')
#将“process1.”粘贴到所有“time”列的名称上
名称(td.wide)=gsub((^[0-9]*$),“process1\\.\\1”,名称(td.wide))
td.wide
id时间不变量2进程1.1进程1.2进程1.3
1 1 6 3.0 4 4.5
2 2 4 3.5不适用
Ya这将是一个问题。我没有使用重塑包,所以不知道所有的选项。这个解决方案只是一个小技巧。另一种方法是用每个id的单个值替换Time不变量列中的值。例如,查找最大值并用max testdata$test Ya替换所有值,这将是一个问题。我没有使用重塑包,所以不知道所有的选项。这个解决方案只是一个小技巧。另一种方法是用每个id的单个值替换Time不变量列中的值。例如,查找最大值并用max testdata$test Ya替换所有值,这将是一个问题。我没有使用重塑包,所以不知道所有的选项。这个解决方案只是一个小技巧。另一种方法是用每个id的单个值替换Time不变量列中的值。例如,查找最大值并用max testdata$test Ya替换所有值,这将是一个问题。我没有使用重塑包,所以不知道所有的选项。这个解决方案只是一个小技巧。另一种方法是用每个id的单个值替换时间不变列中的值。例如,查找最大值