Dear all,
In the following df:
I have several groups (A to C, but could be up to 18 groups). I made a for loop to calculate the median T1 for each group like this:
for(Var in unique(df$Group)) {
assign(paste("T1_", Var, sep = ""), median(filter(df, Group == Var)$T1))
}
I then filter the df, so that I keep only rows from Group A that have a T1 which is +/-1 from the median T1.I have the following code, but I would like to make a for loop to do it in a varying number of groups.
`%+-1%` <- function(T1, T1_A) (T1 >= T1_A-1) & (T1 <= T1_A+1)
df<- df %>% filter ((Group == "A" & T1 %+-1% T1_A) | (Group == "B" & T1 %+-1% T1_B) |
(Group == "C" & T1 %+-1% T1_C))
thanks, could you please explain what the first part does? and also how the second part will filter each group with his respective T1median?
The
group_by
declares that all rows with the same value forGroup
are a group. The second one then calculates the median for these declared groups based on the T1 column, and then filters rows that meet the criteria you mention, being +/- 1 from the groupwise median. This is all tidyverse syntax, which is a convenient grammar for data science, you can learn more here: https://dplyr.tidyverse.org/Basically the verbs (that is how tidyverse calls these standardized functions like filter, mutate, group etc), do a lot of stuff codewise under the hood but for the end user it comes down to learn the limited tidyverse syntax, enabling you to do efficient manipulation of data without lots of code. Base R can often be faster for the same operation, but requires more custom code to write, therefore the tidyverse is often preferrable in analysis scripts to make things short, readable and thereby "tidy".
thanks for all the info. I am trying to adjust it to my data, but it doesn't work or I am missing something. Meanwhile I would really like to find a way to call the "T1_"Var, with a generic name, in order to be able to filter using the +/- of T1_A or T1_B for e.g.
when trying this for e.g.
for (Var in unique(df$Group)) { temp2 <- df %>% filter((Group== Var)$T1 < T1_A)}
it works, but I need to call each respective T1_ for each Group, e.g. for Group==A, then the T1_AIf you add data examples via
dput
and an example desired output we can see to get some code working.Hi, thanks. It's the same data I am talking about and same question. As I could not use your commands, I was trying to find a way... the desired outcome is the same to subset keeping only the rows with T1_"Var"-1<T1<T1_"Var"+1, but as I am newbie I don't know how you can call the T1_"Var", when you have already created the vectors T1_A, T1_B....T1_W