从长格式的不平衡数据计算 post 评估前的可靠改进
Calculate reliable improvement pre to post assesment from unbalanced data in long format
我想在以长格式组织的不平衡数据集中计算从会话 1 到 "the last session" 的可靠改进或恶化。
我的数据是这样的:
ID <- c("A","A","B","B","B","C","C","C","C")
Session <-c(1,2,1,2,3,1,2,3,4)
Value <- c(10,6,25,35,15,20,25,35,35)
Have <- data.table(ID,Session,Value)
Have
ID Session Value
A 1 10
A 2 6
B 1 25
B 2 35
B 3 15
C 1 20
C 2 25
C 3 35
C 4 35
我需要的数据如下所示:
Change <- c(-4,-4,-10,-10,-10,15,15,15,15)
Need <- data.table(ID,Session, Value,Change)
Need
ID Session Value Change
A 1 10 -4
A 2 6 -4
B 1 25 -10
B 2 35 -10
B 3 15 -10
C 1 20 15
C 2 25 15
C 3 35 15
C 4 35 15
我试过这个:
Have$change<-as.vector(unlist(tapply(Have$Value,Have$ID,FUN=function(x){return (x-rep(x[1],length(x)))})));
Have
ID Session Value change
A 1 10 0
A 2 6 -4
B 1 25 0
B 2 35 10
B 3 15 -10
C 1 20 0
C 2 25 5
C 3 35 15
C 4 35 15
我使用了这个 post 中的代码
Calculating change from baseline with data in long format
不是最漂亮的代码,但我认为它可以做你想做的事。我真的不知道 data.table 所以用 dplyr 代替。我还从这个问题
中得到了一些关于如何 select 的帮助
library(dplyr)
Have <- as.data.frame(Have)
Have2 <- Have %>%
group_by(ID) %>%
arrange(Session) %>%
filter(row_number() %in% c( 1 , n() )) %>%
summarise( change = diff(Value) )
Have %>% left_join (Have2 , by = "ID")
编辑:稍微更新了我的代码以简化它
我想在以长格式组织的不平衡数据集中计算从会话 1 到 "the last session" 的可靠改进或恶化。
我的数据是这样的:
ID <- c("A","A","B","B","B","C","C","C","C")
Session <-c(1,2,1,2,3,1,2,3,4)
Value <- c(10,6,25,35,15,20,25,35,35)
Have <- data.table(ID,Session,Value)
Have
ID Session Value
A 1 10
A 2 6
B 1 25
B 2 35
B 3 15
C 1 20
C 2 25
C 3 35
C 4 35
我需要的数据如下所示:
Change <- c(-4,-4,-10,-10,-10,15,15,15,15)
Need <- data.table(ID,Session, Value,Change)
Need
ID Session Value Change
A 1 10 -4
A 2 6 -4
B 1 25 -10
B 2 35 -10
B 3 15 -10
C 1 20 15
C 2 25 15
C 3 35 15
C 4 35 15
我试过这个:
Have$change<-as.vector(unlist(tapply(Have$Value,Have$ID,FUN=function(x){return (x-rep(x[1],length(x)))})));
Have
ID Session Value change
A 1 10 0
A 2 6 -4
B 1 25 0
B 2 35 10
B 3 15 -10
C 1 20 0
C 2 25 5
C 3 35 15
C 4 35 15
我使用了这个 post 中的代码 Calculating change from baseline with data in long format
不是最漂亮的代码,但我认为它可以做你想做的事。我真的不知道 data.table 所以用 dplyr 代替。我还从这个问题
library(dplyr)
Have <- as.data.frame(Have)
Have2 <- Have %>%
group_by(ID) %>%
arrange(Session) %>%
filter(row_number() %in% c( 1 , n() )) %>%
summarise( change = diff(Value) )
Have %>% left_join (Have2 , by = "ID")
编辑:稍微更新了我的代码以简化它