问题
I have a data frame which looks like following and it continues up to subject 22 Beta is the dependent measure.
Subject ROI Block Condition Beta
1 motor1 1 nopred_noom -2.8653
1 motor1 1 pred_noom -2.9126
1 motor1 1 nopred_om -2.8688
1 motor1 1 pred_om -2.9098
1 motor1 1 null -2.7717
1 motor1 2 nopred_noom -2.2382
1 motor1 2 pred_noom -2.0583
1 motor1 2 nopred_om -2.2207
1 motor1 2 pred_om -2.1928
1 motor1 2 null -2.1166
1 motor1 3 nopred_noom -1.5992
1 motor1 3 pred_noom -1.5493
1 motor1 3 nopred_om -1.5230
1 motor1 3 pred_om -1.4851
1 motor1 3 null -1.5624
2 motor1 1 nopred_noom -1.1354
2 motor1 1 pred_noom -1.1614
2 motor1 1 nopred_om -1.2779
2 motor1 1 pred_om -1.1234
2 motor1 1 null -1.2203
2 motor1 2 nopred_noom -1.5728
2 motor1 2 pred_noom -1.6614
2 motor1 2 nopred_om -1.7076
2 motor1 2 pred_om -1.7702
2 motor1 2 null -1.4170
There are 5 conditions, but I want to use the condition null as the baseline and want to subtract it from other conditions in each corresponding block and subject.
so I would subtract Beta in subject 1, block 1, condition "null" from Beta measures in other conditions in subject1, block 1 but then I want to use beta value "null" from subject1, block2 for measures in subject 1, block2 and so on.
null condition occurs every 5 conditions and i suspect I need to use a loop but I am quite new to R and I am not sure how to do this.
any help is appreciated!!! thanks :)
回答1:
This needs the so-called "split-apply-combine" approach. There are many possibilities to do this. The easiest for a beginner is package plyr, because of its nice syntax.
library(plyr)
DF <- ddply(DF, .(Subject,ROI,Block), transform, Beta0 = Beta-Beta[Condition=="null"])
# Subject ROI Block Condition Beta Beta0
# 1 1 motor1 1 nopred_noom -2.8653 -0.0936
# 2 1 motor1 1 pred_noom -2.9126 -0.1409
# 3 1 motor1 1 nopred_om -2.8688 -0.0971
# 4 1 motor1 1 pred_om -2.9098 -0.1381
# 5 1 motor1 1 null -2.7717 0.0000
# 6 1 motor1 2 nopred_noom -2.2382 -0.1216
# 7 1 motor1 2 pred_noom -2.0583 0.0583
# 8 1 motor1 2 nopred_om -2.2207 -0.1041
# 9 1 motor1 2 pred_om -2.1928 -0.0762
# 10 1 motor1 2 null -2.1166 0.0000
# <snip>
回答2:
Here is base R code for the above task:
#-- split
dfs <- split(df, list(df$Block, df$Subject))
#-- apply
Beta0<-NULL
for (i in 1:length(dfs))
{Beta0 <- dfs[[i]]$Beta - dfs[[i]][dfs[[i]]$Condition=="null",]$Beta;
dfs[[i]][,"Beta0"] <- Beta0}
#-- recombine
dfrc <- do.call(rbind, dfs)
df= original data frame; dfs = a list comprising all the split subgroups; dfrc = new data frame that should reproduce the results displayed above for the new column "Beta0".
I posted this because I had a similar data set with - analagously - a missing value of "Beta" for the condition "null" in one block. Plyr produced an error message "arguments imply differing number of rows: x, 0" and di dnot compute. The above code, however, produced NAs for that block but computed all the rest.
回答3:
Getting into data.table
is fun too. Or maybe I'm just on a DT kick...
+1 for an answerable question with data.
df <- read.table(text ='Subject ROI Block Condition Beta
1 motor1 1 nopred_noom -2.8653
1 motor1 1 pred_noom -2.9126
1 motor1 1 nopred_om -2.8688
1 motor1 1 pred_om -2.9098
1 motor1 1 null -2.7717
1 motor1 2 nopred_noom -2.2382
1 motor1 2 pred_noom -2.0583
1 motor1 2 nopred_om -2.2207
1 motor1 2 pred_om -2.1928
1 motor1 2 null -2.1166
1 motor1 3 nopred_noom -1.5992
1 motor1 3 pred_noom -1.5493
1 motor1 3 nopred_om -1.5230
1 motor1 3 pred_om -1.4851
1 motor1 3 null -1.5624
2 motor1 1 nopred_noom -1.1354
2 motor1 1 pred_noom -1.1614
2 motor1 1 nopred_om -1.2779
2 motor1 1 pred_om -1.1234
2 motor1 1 null -1.2203
2 motor1 2 nopred_noom -1.5728
2 motor1 2 pred_noom -1.6614
2 motor1 2 nopred_om -1.7076
2 motor1 2 pred_om -1.7702
2 motor1 2 null -1.4170', header=T)
dt <- data.table(df)
delta_maker <- function(x) {
return(x - x[5])
}
dt[, delta := delta_maker(Beta), by = list(ROI, Subject, Block)]
#Subject ROI Block Condition Beta delta
#1: 1 motor1 1 nopred_noom -2.8653 -0.0936
#2: 1 motor1 1 pred_noom -2.9126 -0.1409
#3: 1 motor1 1 nopred_om -2.8688 -0.0971
#4: 1 motor1 1 pred_om -2.9098 -0.1381
#5: 1 motor1 1 null -2.7717 0.0000
#6: 1 motor1 2 nopred_noom -2.2382 -0.1216
#7: 1 motor1 2 pred_noom -2.0583 0.0583
#8: 1 motor1 2 nopred_om -2.2207 -0.1041
#9: 1 motor1 2 pred_om -2.1928 -0.0762
#10: 1 motor1 2 null -2.1166 0.0000
#11: 1 motor1 3 nopred_noom -1.5992 -0.0368
#12: 1 motor1 3 pred_noom -1.5493 0.0131
#13: 1 motor1 3 nopred_om -1.5230 0.0394
#14: 1 motor1 3 pred_om -1.4851 0.0773
#15: 1 motor1 3 null -1.5624 0.0000
#16: 2 motor1 1 nopred_noom -1.1354 0.0849
#17: 2 motor1 1 pred_noom -1.1614 0.0589
#18: 2 motor1 1 nopred_om -1.2779 -0.0576
#19: 2 motor1 1 pred_om -1.1234 0.0969
#20: 2 motor1 1 null -1.2203 0.0000
#21: 2 motor1 2 nopred_noom -1.5728 -0.1558
#22: 2 motor1 2 pred_noom -1.6614 -0.2444
#23: 2 motor1 2 nopred_om -1.7076 -0.2906
#24: 2 motor1 2 pred_om -1.7702 -0.3532
#25: 2 motor1 2 null -1.4170 0.0000
#Subject ROI Block Condition Beta delta
来源:https://stackoverflow.com/questions/17447120/subtracting-a-specific-condition-for-each-measure