3 가지 방법 (method1, method2, method0)을 사용하여 2 개의 사이트 (A & B), 2 개의 그룹 (그룹 1 & 2)의 추정치를 포함하는 데이터 프레임 df가 있습니다.컨트롤 또는 기준 그룹이있는 데이터 프레임에서 R 계산
df1<-data.frame(site=rep("A", 21),
group=rep("group1", 21),
estimate=c(rnorm(10, 15, 3), rnorm(10, 2, 7), rnorm(1, 6, 2)),
method=c(rep(c("method1","method2"),each=10),"method0"))
df2<-data.frame(site=rep("B", 21),
group=rep("group2", 21),
estimate=c(rnorm(10, 13, 3), rnorm(10, 5, 7), rnorm(1, 9, 2)),
method=c(rep(c("method1","method2"),each=10),"method0"))
df<-rbind(df1, df2)
df
site group estimate method
1 A group1 15.1561073 method1
2 A group1 14.4067422 method1
3 A group1 12.7428921 method1
..........
41 B group2 0.3548033 method2
42 B group2 10.5820482 method0
I 기준선 그룹으로 method0를 사용하여 각 사이트/그룹에 대한 각각의 추정 상대 백분율 편차 (RB)을 계산하고 싶다.
#for each site and group of estimate
rb<-(estimate-estimate0)/estimate0*100%
# where estimate0 is the estimate of method0 of that certain site/group
각 사이트/그룹에는 하나만 추정됩니다. 나는 간단한 함수를 작성하고 각 사이트/그룹에 apply
을 사용하려고했지만 제대로 작동하지 않았습니다.
fun.rb<-function(df, basline){
control<-df$method==baseline
rb<-(df$estimate-control$estimate)/(control$estimate)*100%
return(rb)
}
df %>% group_by(site,group) %>% mutate(rb=fun.rb, baseline="method0")
모든 입력과 의견은 크게 감사하겠습니다.
와우, 좋아 보인다. 내 기능 수정에 감사드립니다. – lamushidi
필자는 실제로 함수가 필요 없다는 것을 지적해야하며 함수 호출을 함수 내부의 계산으로 바꾸면 전체를 인라인으로 처리 할 수 있습니다. – yeedle
물론입니다. 그러면 코드가 더 깔끔해질 것입니다. 고마워. – lamushidi