Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Partial grouping inside a dataframe in R

For statistical analysis purpose, I would like to regroup some rows inside a data frame based on their values.

What I have:

number latitude
30 57
12 59
01 68
12 66
101 55
47 61
05 60
288 67

The desired output would be, for example, to regroup every latitude above 66 (66+67+68) in a single category 66+ and the desired output would be like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

number latitude new
30 57 57
12 59 59
01 68 66+
12 66 66+
101 55 55
47 61 61
05 60 60
288 67 66+

I do not want to use an if loop because I feel that it is not really R friendly.
I would also like to keep the initial column, that way I can try different combinations later on.

Thank you very much.

>Solution :

Option mutate and ifelse:

library(dplyr)
df %>%
  mutate(new = ifelse(latitude >= 66, "66+", latitude))

Output:

  number latitude new
1     30       57  57
2     12       59  59
3     01       68 66+
4     12       66 66+
5    101       55  55
6     47       61  61
7     05       60  60
8    288       67 66+

Data

df <- data.frame(number = c("30","12","01","12","101","47","05","288"),
                 latitude = c(57,59,68,66,55,61,60,67))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading