I have this dataset
> dput(head(data_negctr1, 50))
structure(list(ID = c("01", "01", "01", "01", "01", "01", "02",
"02", "02", "02", "04", "04", "04", "04", "04", "04", "06", "06",
"06", "06", "06", "06", "07", "07", "07", "07", "07", "07", "08",
"08", "08", "08", "08", "08", "09", "09", "09", "09", "10", "10",
"10", "10", "10", "10", "11", "11", "11", "11", "11", "11"),
COND = c("NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR", "NEG-NOC", "NEG-CTR",
"NEG-NOC", "NEG-CTR", "NEG-NOC"), SES = c("L", "L", "R",
"R", "V", "V", "L", "L", "R", "R", "L", "L", "R", "R", "V",
"V", "L", "L", "R", "R", "V", "V", "L", "L", "R", "R", "V",
"V", "L", "L", "R", "R", "V", "V", "L", "L", "V", "V", "L",
"L", "R", "R", "V", "V", "L", "L", "R", "R", "V", "V"), `LPP2(1000-1500).FCz` = c(-0.397250087672501,
1.01519755373062, 0.663115370014327, -1.72160165493474, -0.198736254963744,
-3.16101041766438, 9.21248347391488, 4.2029104966206, 4.43538787409554,
1.28346027317076, 3.21881665778703, 5.92604591551597, 2.18006640865321,
-0.225533814334514, 3.11042068112836, 2.27800090558473, 2.11741401205978,
2.41324857857743, 1.84146702695206, 2.1796529384155, 0.70169995101623,
1.75290674648806, 5.54833202313032, 6.28414577615156, -0.243931314877871,
4.36296654230765, 6.69246804853767, 4.76363910849669, 2.51614439050594,
0.544519979811557, 7.01169149718639, -0.0444060583672976,
1.37744267413711, 0.973396686787801, 4.96421236672541, 7.15336406198256,
4.70492544977759, 12.1173859570715, 3.2659641826697, 2.36748010754166,
-1.3357207368765, -0.0920228462827166, -3.27424811388479,
3.17906097432267, 5.63727876507353, 1.12124233903887, 5.60016620262074,
-0.948471360524649, -0.548079192716476, 4.61956745266038),
`LPP2(1000-1500).Cz` = c(2.91913936481813, 1.85489998202138,
3.95694572160701, 2.36649557381131, 2.96437294922766, -2.12913230708907,
10.0673617448764, 5.28032844958354, 4.54790063971537, 2.16569592764593,
2.58426832963409, 5.077230852852, 6.61872855398538, -1.21696526578647,
3.44844607014521, 3.02403433835637, 1.31359485387859, 3.04464581710327,
-1.66589658134171, 2.27922842547595, -0.119440173019054,
0.668265457194833, 7.62396496656744, 8.44945750972713, -0.147829603799427,
4.04101914581768, 7.32640154038155, 6.75063445570149, 3.24583129375449,
-0.71967320732169, 6.0531518403553, -0.610046927519558, 0.716894061961362,
1.15587576192688, 6.44199941531222, 8.83328750324128, 6.44896958389742,
10.5178519351064, 5.72982692246142, 5.81594496182059, 1.83049907582528,
3.33758848411935, -0.881527211868382, 4.99936090838488, 8.6569554790885,
-1.00196034198877, 6.68708434240297, -1.05436002526955, 0.914180881345654,
4.57223746317305), `LPP2(1000-1500).Pz` = c(8.38141924882334,
4.88842858528025, 3.88831364571248, 5.83201076558185, 6.28027312932027,
5.24535230966772, 8.55827491190151, 4.37390045274906, 6.04702803069286,
5.29458007289059, 6.30887598671621, 5.9614279900414, 3.66646157996528,
0.240956222277802, 1.08242973465635, 2.99896314000211, 2.79229514887897,
5.44503756798021, 4.18316343024083, 7.57635217480856, 1.22060069796986,
0.43007709818575, 10.1988511186129, 6.91943531570565, 4.85299225301444,
4.65269577391896, 8.73650967214853, 7.20593389743811, 0.0275313390411946,
-2.25099759243389, 4.84396306101255, 0.490818836727051, -1.33620384654018,
-1.28885901608471, 10.2129994727413, 10.8178803270846, 8.11715753014206,
13.3060153720352, 7.12058567533851, 7.32756919594656, 4.44482623240119,
3.96749443325504, 2.88799699943079, 5.57921718107867, 5.26994720706095,
0.150423100714413, 6.7884358866985, 1.43104145556053, -1.35508717090199,
5.24004334238142), `LPP2(1000-1500).POz` = c(9.61455213192824,
7.21299011910717, 6.46723813995257, 7.25962635499353, 7.91468942320841,
9.94838815736199, 5.76901446016799, 1.63411653734436, 4.09091078261671,
5.32593378182311, 6.86490468078958, 5.26280996552585, 5.0384350436334,
0.361670119961531, 1.07455889922813, 1.65917850515029, 4.15964501337974,
5.35297505953096, 4.11643541344308, 6.89982254066399, 0.742475871599007,
3.46485267529148, 7.78087935610006, 5.56652323496618, 4.7450495762156,
6.10110636998382, 4.48645178533554, 6.25868881853194, -0.823830529987789,
-2.33362938716716, 2.65023738859414, -1.22376317353831, -2.42743430604504,
-1.97143573568321, 6.51666682131685, 9.0739717272552, 0.649335906601939,
10.4599728475769, 3.117902050049, 5.32174579379253, -0.0968694240545827,
1.23896069355975, 1.10176599828887, 4.64632804145351, 4.53436293179661,
-0.736223349923721, 5.48908879966524, 3.07114450173319, -2.06826278615017,
0.745452283454849)), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"))
>
As you could see data is in a wide format and I was asked to compute difference between the two conditions levels. If I would need to create a further column/dataset reporting coupled difference between value and conditionated to each condition level (NEG-CTR - NEU-NOC), what am I supposed to do?
CodePudding user response:
Perhaps we need a group by difference and then create new columns - grouped by 'ID', 'SES', loop across
the numeric columns in mutate
, subset the values by creating a logical vector based on the 'COND' values, get the difference (-
) and add new columns by modifying the .names
i.e. by adding a suffix _diff
to the original column names
library(dplyr)
data_negctr2 <- data_negctr1 %>%
group_by(ID, SES) %>%
mutate(across(where(is.numeric), ~ .[COND == 'NEG-CTR'] -
.[COND == 'NEG-NOC'], .names = "{.col}_diff")) %>%
ungroup
-output
> print(head(data_negctr2, 2), Inf)
# A tibble: 2 × 11
ID COND SES `LPP2(1000-1500).FCz` `LPP2(1000-1500).Cz` `LPP2(1000-1500).Pz` `LPP2(1000-1500).POz` `LPP2(1000-1500).FCz_diff`
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 01 NEG-CTR L -0.397 2.92 8.38 9.61 -1.41
2 01 NEG-NOC L 1.02 1.85 4.89 7.21 -1.41
`LPP2(1000-1500).Cz_diff` `LPP2(1000-1500).Pz_diff` `LPP2(1000-1500).POz_diff`
<dbl> <dbl> <dbl>
1 1.06 3.49 2.40
2 1.06 3.49 2.40