putting together 2 groups of subjects

Hi there, probably a very simple question for R users
I have a dataset such as:

subject Group variable
AAA 1 1.5
BBB 2 3.1
CCC 3 3.9
DDD 4 1.2

I'd like to put together people from group 3 and group 4. How could I do that with R?
p.s. using RStudio and if you could give a bit of explanation on how and why doing it in a certain way, it would be amazing thank you :slight_smile:

Thank you!

Hi Morello,
In the way that you helpfully provided an example of the sort of data you would start with. i.e.

could you similarly give an example of how the same would look after you applied the transformations you would like us to help you create ? It is ambigious to me what 'put together people from group 3 and group 4' might mean

1 Like

Thank you Nirgrahamuk. I aologize for my bad phrasing, English is not my native language and I'm not used to programming language.

So basically what I would like to do is to get the participants that have value "3" and "4" under the variable "group" to all have value "3" with one line of coding (conceptually speaking is like opening tha data.frame in excel outside R, and assigning value "3" to all the subjects with value 4 in the column "Group")

Outcome should be:
subject Group variable
AAA 1 1.5
BBB 2 3.1
CCC. 3 3.9
DDD 3 1.2

If that is the only value you want to change you can simply do something like this


sample_df <- data.frame(
  stringsAsFactors = FALSE,
           subject = c("AAA", "BBB", "CCC", "DDD"),
             Group = c(1, 2, 3, 4),
          variable = c(1.5, 3.1, 3.9, 1.2)

sample_df %>% 
    mutate(Group = if_else(Group == 4, 3, Group))
#>   subject Group variable
#> 1     AAA     1      1.5
#> 2     BBB     2      3.1
#> 3     CCC     3      3.9
#> 4     DDD     3      1.2

Created on 2020-02-05 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.