Home > other >  How do I convert a numeric table of that contains percentages in a Data Frame in R to a numeric tabl
How do I convert a numeric table of that contains percentages in a Data Frame in R to a numeric tabl

Time:02-01

Sorry everyone if this is already in another post, but I am totally new here and super green with R.

So what I am trying to achieve is that all values from a table with percentages above or equal to 15% should become a 1 and all values below 15% should become 0

I was thinking of gsub but can the "to be replaced" be a condition?

CodePudding user response:

You could do:

library(dplyr)
data %>% 
   across(everything(), ~ case_when(.x >= 15 ~ 1, TRUE ~ 0))

CodePudding user response:

Using dplyr:

library(dplyr)

df <- data.frame(perc = c(0.1, 0.4, 0.13, 0.8))
df %>%
  mutate(perc = ifelse(perc < 0.15, 0, 1))
  •  Tags:  
  • Related