mutate()
La función en R Language se usa para agregar nuevas variables en un marco de datos que se forman al realizar operaciones en variables existentes.
Sintaxis: mutate(x, expr)
Parámetros:
x: Data Frame
expr: operación en variables
Ejemplo 1:
# R program to add new variables # in a data frame # Loading library library(dplyr) # Create a data frame d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"), age = c(7, 5, 9, 16), ht = c(46, NA, NA, 69), school = c("yes", "yes", "no", "no") ) # Calculating a variable x3 which is sum of height # and age printing with ht and age mutate(d, x3 = ht + age)
Producción:
name age ht school x3 1 Abhi 7 46 yes 53 2 Bhavesh 5 NA yes NA 3 Chaman 9 NA no NA 4 Dimri 16 69 no 85
Ejemplo 2:
# R program to add new variables # in a data frame # Loading library library(dplyr) # Create a data frame d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"), age = c(7, 5, 9, 16), ht = c(46, NA, NA, 69), school = c("yes", "yes", "no", "no") ) # Calculating a variable x3 which is product of height # and age printing with ht and age mutate(d, x3 = ht * age)
Producción:
name age ht school x3 1 Abhi 7 46 yes 322 2 Bhavesh 5 NA yes NA 3 Chaman 9 NA no NA 4 Dimri 16 69 no 1104
Publicación traducida automáticamente
Artículo escrito por nidhi_biet y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA