R语言计算特定列的和(自备)

R语言长款数据转换(自备)_r语言宽数据转换成长数据-CSDN博客

数据
复制代码
rm(list = ls()) 
library(ggplot2)
library(ggpubr)
library(cowplot)
data <- iris##鸢尾花数据集
#[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"   
计算

先整合指定计算的列名,然后使用**rowSums(data[,列])**进行计算

复制代码
#计算特定列的和
col1 <-  c("Sepal.Length","Sepal.Width")
data$col1 <- rowSums(data[,col1])

col2 <-  c("Petal.Length","Petal.Width")
data$col2 <- rowSums(data[,col2])
复制代码
head(data)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species col1 col2
1          5.1         3.5          1.4         0.2  setosa  8.6  1.6
2          4.9         3.0          1.4         0.2  setosa  7.9  1.6
3          4.7         3.2          1.3         0.2  setosa  7.9  1.5
4          4.6         3.1          1.5         0.2  setosa  7.7  1.7
5          5.0         3.6          1.4         0.2  setosa  8.6  1.6
6          5.4         3.9          1.7         0.4  setosa  9.3  2.1

参考:R 数据框的选定列的总和 | D栈 - Delft Stack

相关推荐
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端
norlan_jame9 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054969 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月9 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237179 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian9 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡9 天前
简单工厂模式
开发语言·算法·c#