Apply Function to Each Cell of Data Frame in R (2 Examples) | All Elements | BaseR vs. dplyr Package

preview_player
Показать описание
R code of this video:

x2 = letters[5:1],
x3 = 7)

my_fun <- function(x) { # Create user-defined function
paste0("out_", x)
}

my_fun(data$x1[3]) # Apply function to one value

data_new1 <- data # Duplicate data frame
data_new1[] <- lapply(data_new1, my_fun) # Apply function to each element

library("dplyr")

data_new2 <- data %>% # Apply function to each element
mutate_all(my_fun)

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Great stuff, awesome tool to have using both Base R and dplyr :)

nth.education