user11916948
user11916948

Reputation: 954

Set several conditions in a fucntion

For each sequence I want to get the difference between A/A0 and B/B0 while considering id. I made a function to run this per sequence. But I could I do to be able to also change trt in the function?

rm(list=ls())
    id <- rep(1:6, each=4)
    trt <- c("A","A0","B","B0","B","B0","A","A0","A","A0","B","B0","B","B0","A","A0","A","A0","B","B0","B","B0","A","A0")
    seq <- rep(rep(1:2,each=2),6)
    set.seed(1)
    q <- runif(24)
    df <- data.frame(id,trt,seq,q)
    df

 id trt seq          q
1   1   A   1 0.26550866
2   1  A0   1 0.37212390
3   1   B   2 0.57285336
4   1  B0   2 0.90820779
5   2   B   1 0.20168193
6   2  B0   1 0.89838968
7   2   A   2 0.94467527
8   2  A0   2 0.66079779
9   3   A   1 0.62911404
10  3  A0   1 0.06178627
11  3   B   2 0.20597457
12  3  B0   2 0.17655675
13  4   B   1 0.68702285
14  4  B0   1 0.38410372
15  4   A   2 0.76984142
16  4  A0   2 0.49769924
17  5   A   1 0.71761851
18  5  A0   1 0.99190609
19  5   B   2 0.38003518
20  5  B0   2 0.77744522
21  6   B   1 0.93470523
22  6  B0   1 0.21214252
23  6   A   2 0.65167377
24  6  A0   2 0.12555510

test <- function(i) {
  df1 <- df[df$seq==i,]
  dfA <-  df1[df1$trt %in% c("A", "A0"),] 
  dfA <- dfA[,c(1,2,4)]
  library(tidyr)
  colnames(dfA)
  dfA <- spread(dfA,trt,q)
  dfA <- c(dfA[,2]-dfA[,3])
  return(dfA)
}
test(1)
test(2) 

I was thinking something like this but it doent work

test <- function(i,df$trt[m],df$trt[n]) {
      df1 <- df[df$seq==i,]
      dfA <-  df1[df1$trt %in% c(m, n),] 
      dfA <- dfA[,c(1,2,4)]
      library(tidyr)
      colnames(dfA)
      dfA <- spread(dfA,trt,q)
      dfA <- c(dfA[,2]-dfA[,3])
      return(dfA)
    }
test(1,2,3)

Upvotes: 0

Views: 31

Answers (2)

akrun
akrun

Reputation: 887068

An option with data.table

library(data.table)
setDT(df)[order(id, trt),  .(q = diff(q)), .(id, seq)]

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388962

We can arrange the data by id and trt so that A0 is after A and same for B, group_by id and seq and calculate the difference.

library(dplyr)

df %>% arrange(id, trt) %>% group_by(id, seq) %>% summarise(q = diff(q))

Upvotes: 1

Related Questions