为每个度量减去特定条件 [英] subtracting a specific condition for each measure
问题描述
我有一个如下所示的数据框,它一直持续到主题 22Beta 是依赖度量.
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
有 5 个条件,但我想使用条件 null 作为基线,并希望从每个相应块和主题中的其他条件中减去它.
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.
所以我会从主题 1 块 1 中其他条件的 Beta 度量中减去主题 1 块 1 中的 Beta 值,块 1 但然后我想使用主题 1 块 2 中的 beta 值null"作为主题 1 中的度量、block2 等等.
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.
每 5 个条件出现一次空条件,我怀疑我需要使用循环,但我对 R 很陌生,我不知道如何做到这一点.
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 :)
推荐答案
以下是上述任务的基本 R 代码:
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=原始数据框;dfs = 包含所有拆分子组的列表;dfrc = 新数据框,应重现上面为新列Beta0"显示的结果.
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".
我发布这个是因为我有一个类似的数据集,其中一个块中条件null"的缺失值Beta".Plyr 产生了一条错误消息参数意味着不同的行数:x, 0"并且没有计算.然而,上面的代码为该块生成了 NA,但计算了所有其余的.
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.
这篇关于为每个度量减去特定条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!