Reputation: 368
My question seems really simple, and indeed I feel very annoyed for the fact that I cannot make it work. Let's say I have a simple dataframe
with one column for group
and one variable x
. Because my variable group contains a "control" condition, I would like to run a t.test
of all my other conditions against my control variable.
library(data.table) # I am use to the data.table sintax, tho I will happily accept a solution in any other dialect
# Generate dummy data
set.seed(1)
df <- data.table(x = rnorm(100), g = sample(LETTERS[1:3], size = 100, replace =T ))
setkey(df, g, x) # Order
df # Inspect data
For that purpose, I would like to dcast
the control group and add it as a new column. Since what I want is to run a t-test and for it, I will use the whole group, I do not mind in which order the column gets included. However, the function that I would use to change from a long format to a wide format (dcast
), doesn't seem to work here.
# dcast appoach
m <- dcast(df, x ~ g) # This is just... B*#!!it
So here is an approximation of what I look for:
# Kind of what I want
# Isolate control condition
Control <- df[g == "C"]
df[, C := rep(Control, 3)] # In this case it says there a "remainder", tho I would prefer to add NAs to the variable x until completion
I also would not mind having all the groups A, B and C, as columns.
Thanks in advance for your help
Upvotes: 0
Views: 108
Reputation: 42544
Perhaps, this might be what the OP has asked for:
library(data.table)
dcast(df, rowid(g) ~ g, value.var = "x")
g A B C 1: 1 -1.804958629 -1.98935170 -2.21469989 2: 2 -1.470752384 -1.52356680 -0.74327321 3: 3 -1.276592208 -1.37705956 -0.62124058 4: 4 -1.253633400 -1.12936310 -0.61202639 5: 5 -1.224612615 -1.04413463 -0.58952095 6: 6 -0.934097632 -0.83562861 -0.47340064 7: 7 -0.709946431 -0.82046838 -0.41499456 8: 8 -0.707495157 -0.68875569 -0.39428995 9: 9 -0.626453811 -0.47815006 -0.30538839 10: 10 -0.573265414 -0.25336168 -0.13505460 11: 11 -0.568668733 -0.13517862 0.02800216 12: 12 -0.542520031 -0.11234621 0.39810588 13: 13 -0.443291873 -0.05931340 0.41794156 14: 14 -0.367221476 -0.05612874 0.55848643 15: 15 -0.304183924 -0.05380504 0.61982575 16: 16 -0.164523596 -0.01619026 0.69696338 17: 17 -0.155795507 0.07434132 0.82122120 18: 18 -0.102787727 0.15325334 0.88110773 19: 19 -0.044933609 0.34111969 0.94383621 20: 20 -0.039240003 0.36458196 1.12493092 21: 21 0.001105352 0.38767161 1.16040262 22: 22 0.074564983 0.48742905 1.17808700 23: 23 0.183643324 0.56971963 1.46555486 24: 24 0.188792300 0.59390132 1.51178117 25: 25 0.267098791 0.61072635 NA 26: 26 0.291446236 0.76317575 NA 27: 27 0.329507772 1.10002537 NA 28: 28 0.332950371 1.35867955 NA 29: 29 0.370018810 1.43302370 NA 30: 30 0.389843236 1.58683345 NA 31: 31 0.475509529 2.40161776 NA 32: 32 0.556663199 NA NA 33: 33 0.575781352 NA NA 34: 34 0.593946188 NA NA 35: 35 0.689739362 NA NA 36: 36 0.700213650 NA NA 37: 37 0.738324705 NA NA 38: 38 0.768532925 NA NA 39: 39 0.782136301 NA NA 40: 40 0.918977372 NA NA 41: 41 1.063099837 NA NA 42: 42 1.207867806 NA NA 43: 43 1.595280802 NA NA 44: 44 1.980399899 NA NA 45: 45 2.172611670 NA NA g A B C
This works by artificially introducing an individual row count rowid(g)
for each group.
However, in line with 42-'s comment, I do not understand how this will help to solve OP's underlying problem.
Upvotes: 1