mercredi 11 août 2021

Replace all values in a data frame, conditionally

I have a numeric data frame, and I'm trying to change each value in it to a string and then summarize it, proportionally, by that string.

Example table:

var1 var2 var3 var4
 2    3    5    8
 3    6    8    7
 5    3    7    4

If a number is less than 4, I would like to replace it with "no", and if a number is 4 or greater, I would like to replace it with "yes"

I then would like to summarize the proportion of "yes" and "no" values for each column.

I've tried using dplyr, and it seems easy to do for a single column, but I'm having trouble applying it across all columns. I tried an approach like this, but didn't have success.

df %>%
select(var1:var4) %>%
mutate_each(funs(replace(., . < 4, "no")) %>%
mutate_each(funs(replace., . => 4, "yes")) %>%
summarise(count = n())

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire