首页 > 其他分享 >【807】R语言mutate函数

【807】R语言mutate函数

时间:2023-02-27 18:56:28浏览次数:42  
标签:mutate 函数 no age ht x3 yes 807

参考:R语言 mutate()实例讲解

mutate()R语言中的函数用于在 DataFrame 中添加新变量,这些变量是通过对现有变量进行操作而形成的。

用法: mutate(x, expr)

参数:
x:数据帧
expr:对变量的操作

举例:通过操作列进行创建新的列!

> d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),  
+                  age = c(7, 5, 9, 16),  
+                  ht = c(46, 23, 34, 69), 
+                  school = c("yes", "yes", "no", "no") ) 
> d
     name age ht school
1    Abhi   7 46    yes
2 Bhavesh   5 23    yes
3  Chaman   9 34     no
4   Dimri  16 69     no
> mutate(
+     d, 
+     x3 = ht + age,
+     x4 = ht * age,
+     x5 = x3 + x4
+     )  
     name age ht school x3   x4   x5
1    Abhi   7 46    yes 53  322  375
2 Bhavesh   5 23    yes 28  115  143
3  Chaman   9 34     no 43  306  349
4   Dimri  16 69     no 85 1104 1189

 

标签:mutate,函数,no,age,ht,x3,yes,807
From: https://www.cnblogs.com/alex-bn-lee/p/17161473.html

相关文章