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

相关推荐
Acrelhuang5 分钟前
工商业用电成本高?安科瑞液冷储能一体机一站式解供能难题-安科瑞黄安南
大数据·开发语言·人工智能·物联网·安全
hello 早上好6 分钟前
03_JVM(Java Virtual Machine)的生命周期
java·开发语言·jvm
沐雪架构师7 分钟前
LangChain 1.0 Agent开发实战指南
开发语言·javascript·langchain
tod1138 分钟前
力扣高频 SQL 50 题阶段总结(四)
开发语言·数据库·sql·算法·leetcode
2501_9400078919 分钟前
Flutter for OpenHarmony三国杀攻略App实战 - 战绩记录功能实现
开发语言·javascript·flutter
naruto_lnq19 分钟前
C++中的桥接模式
开发语言·c++·算法
无限进步_20 分钟前
面试题 02.02. 返回倒数第 k 个节点 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
布谷歌29 分钟前
面试题整理
java·开发语言
j4455661137 分钟前
C++中的职责链模式高级应用
开发语言·c++·算法
Hello World . .39 分钟前
数据结构:栈和队列
c语言·开发语言·数据结构·vim