csh sum算总和_如何在R中使用sum()–在R中查找元素的总和

本文介绍了如何在R中使用sum()函数计算向量、特定列及行的总和,并展示了如何处理包含'NA'值的情况。
摘要由CSDN通过智能技术生成

csh sum算总和

Let’s learn how to find the sum of the values with the help of the sum() in R. In this tutorial, we will try to find the sum of the elements of the vector.

让我们学习如何借助R中的sum()查找值的总和 。在本教程中,我们将尝试查找向量元素的总和。

The syntax of the sum() function is = sum(x,na.rm=FALSE/TRUE)

sum()函数的语法为= sum(x,na.rm = FALSE / TRUE)

Vector is the easiest method to store multiple elements in R. Look at the below examples which show the various types of vectors.

向量是在R中存储多个元素的最简单方法。请看以下示例,其中显示了各种类型的向量。


Ex_vector:  
V<- c(2,4,6,8,10)  #This is a numerical vector  
V<-c('red', 'blue', 'orange')    #This is a character or string vector 
V<-c(TRUE, FALSE,TRUE)   #This is a logical vector


R中sum()的基本用法 (Basic usage of sum() in R)

In this section, we are finding the sum of the given values. Execute the below code to find the sum of the values.

在本节中,我们将找到给定值的总和。 执行以下代码以找到值的总和。


#list of values or a vector having numerical values
df<- c(23,44,66,34,56,78,97,53,24,57,34,678,643,1344)

#calculates the sum of the values 
sum(df)

Output —> 3231

输出—> 3231



使用sum()函数时跳过“ NA”值 (Skip “NA” values when using the sum() function)

Sometimes your dataset may contain ‘NA” values i.e. ‘Not Available’. So if you add the values including NA, the sum() functions return the NA instead of numerical summation output.

有时,您的数据集可能包含“ NA”值,即“不可用” 。 因此,如果添加包含NA的值,sum()函数将返回NA而不是数字求和输出。

Let’s learn how to deal with such datasets.

让我们学习如何处理此类数据集。

In this section, we are finding the sum of the vectors having the numeric values along with the value ‘NA. The syntax of the sum() function shows that,

在本节中,我们将找到具有数字值和值'NA的向量的总和。 sum()函数的语法表明,

sum(x,na.rm=FALSE/TRUE)

sum(x,na.rm=FALSE/TRUE)

  • x-> it is the vector having the numeric values.

    x->是具有数值的向量。
  • na.rm-> This asks for remove or returns ‘NA’. If you made it TRUE, then it skips the NA in the vector, otherwise, NA will be calculated.

    na.rm->这要求删除或返回“ NA”。 如果将其设置为TRUE,则它将跳过向量中的NA,否则将计算NA。

The below code will illustrate the action.

下面的代码将说明该操作。


#creates a vector having numerical values
x<-c(123,54,23,876,NA,134,2346,NA)

#calculates the sum and removes the NA values from the summation
sum(x,na.rm = TRUE)

Output —> 3556

输出—> 3556


#if you mention FALSE, the sum function returns the value NA 
sum(x,na.rm = FALSE)
----> NA


在R中使用sum()来添加特定列的值 (Using sum() in R to add values of a specific column)

Summing the values present in the particular column is very easy in R. The below code will illustrate the same.

在R中,汇总特定列中存在的值非常容易。以下代码将对此进行说明。

This dataset contains the ‘NA’ value. So we are handling it by using na.rm=TRUE functon as shown in the code.

该数据集包含“ NA”值。 因此,我们使用na.rm = TRUE函数来处理它,如代码所示。


#read the data 
datasets::airquality

#sample data, just a few samples
    Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
3      12     149 12.6   74     5   3
4      18     313 11.5   62     5   4
5      NA      NA 14.3   56     5   5
6      28      NA 14.9   66     5   6
7      23     299  8.6   65     5   7
8      19      99 13.8   59     5   8
9       8      19 20.1   61     5   9
10     NA     194  8.6   69     5  10  continues.....

#calculates the summation of the values in column 'Ozone'.
sum(airquality$Ozone, na.rm = TRUE)

Output —> 4887

输出—> 4887



在R中独立汇总所有行中的所有数据 (Sum all the data in all the rows independently in R)

This section focuses on summing each row present in the dataset. Execute the below code to get the summed values of each row.

本节着重于汇总数据集中存在的每一行 。 执行以下代码以获取每一行的总和。

Here we are removing the NA values by na.rm=TRUE function.

在这里,我们通过na.rm = TRUE函数删除NA值。


datasets::airquality

rowSums(airquality, na.rm = TRUE)

Output: You can see the summation of all values present in each row.

输出:您可以看到每一行中所有值的总和。


[1] 311.4 241.0 255.6 413.5 80.3 119.9 407.6 203.8 122.1 286.6 103.9 367.7
[13] 394.2 385.9 174.2 444.5 441.0 182.4 455.5 151.7 103.7 447.6 127.7 226.0
[25] 169.6 369.9 97.0 148.0 426.9 457.7 435.4 379.6 378.7 334.1 289.2 324.6
[37] 369.3 260.7 380.9 480.8 476.5 379.9 369.2 280.0 445.8 433.5 325.9 436.7
[49] 155.2 241.5 262.3 260.3 164.7 200.6 362.3 249.0 245.0 163.3 223.5 157.9
[61] 265.0 500.1 400.2 368.2 206.9 338.6 460.9 460.1 477.3 482.7 373.4 247.6
[73] 380.3 317.9 417.9 171.3 418.9 425.3 461.3 384.1 406.5 131.9 377.7 418.5
[85] 499.6 456.0 224.6 266.0 425.4 454.4 444.4 441.2 218.9 137.8 193.4 182.9
[97] 140.4 171.6 485.0 434.3 432.0 340.6 253.5 353.5 415.5 333.7 177.5 204.3
[109] 220.3 247.4 390.9 350.3 401.5 161.3 373.6 377.7 523.4 416.0 281.7 421.7
[121] 476.3 461.3 412.3 370.9 383.1 363.8 390.6 250.4 238.5 378.9 348.3 354.9
[133] 384.7 395.9 392.5 371.3 137.9 231.5 392.9 348.8 153.3 368.3 336.0 357.6
[145] 148.2 298.3 168.3 147.6 334.9 271.2 331.3 271.0 361.5


查找数据集所有列的总和 (Finding the sum of all the columns of the dataset)

Let’s find the sum of each column present in the dataset. Execute the below code to find the sum of each column.

让我们找到数据集中存在的每一列总和 。 执行以下代码以查找每一列的总和。


dataseta::airquality

colSums(airquality, na.rm = TRUE)

Output:

输出:


Ozone      Solar.R       Wind        Temp        Month       Day
4887.0      27146.0     1523.5      11916.0     1070.0       2418.0


结语 (Wrapping up)

The sum() function in R to find the sum of the values in the vector. This tutorial shows how to find the sum of the values, the sum of a particular row and column, and also how to get the summation value of each row and column in the dataset.

R中的sum()函数可找到向量中值的总和。 本教程说明如何找到值的总和,特定行和列的总和,以及如何获取数据集中每个行和列的总和。

The important thing is to consider the NA values or not. If you want to eliminate it, mention TRUE otherwise it should be FALSE as shown above. That’s all for now, keep going!!!

重要的是要考虑NA值与否。 如果要消除它,请提及TRUE,否则应为FALSE,如上所示。 现在就这些,继续前进!!!

翻译自: https://www.journaldev.com/39197/sum-in-r

csh sum算总和

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值