Back to: R plain language help files Author: Van Wishingrad sum: Sum of Vector Elements Description The function sum adds together a series of numeric, complex or logical vectors. Usage sum(..., na.rm = FALSE) Arguments ... numeric, complex or logical vectors being evaluated na.rm parameter that specifies whether or not missing values should be ignored (=TRUE or =FALSE). Details Logical true values are treated as one, false values are treated as zero. By default na.rm = FALSE. Missing values (including NaN) are included in the summation, resulting in an output error. If na.rm = TRUE, then missing values (including NaN) are ignored and the output contains the summation of logical values only. Value If the argument being evaluated is an integer or a logical vector, the output is an integer. If the argument being evaluated is complex, then the output is numeric or complex. Sources of error If an NA is returned, then there exists an empty value in the dataset. Set na.rm = TRUE to correct this error. If NaN is returned, then a value of your dataset is not a real number. Set na.rm = TRUE to correct this error. If “sum not meaningful for factors” is returned, then the vector attempting to be summed may be a factor, or may be treated as a factor. Applying the function as.numeric to the variable being summed may correct this error. Loading a dataset that uses “,” as a decimal symbol instead of “.” may also cause this error. If this is the case reload the dataset indicating the correct symbol to be interpreted as a decimal: dataset <- read.table("clipboard", header = TRUE, dec=",") References Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Crawley, M.J. (2007) The R Book. John Wiley & Sons Ltd. Wadsworth & Brooks/Cole. R-help page sum {base} http://rss.acs.unt.edu/Rdoc/library/base/html/sum.html See Also colSums for column sums rowSums for row sums complex for details about complex vectors S4groupGeneric for details about logical operators Examples ##for numeric vectors #adding together a vector of individual specified #values• sum(1,2,3,4,5) #adding together a vector of a range of specified #values• sum(1:5) #adding together a vector of a range of individual and #specified values sum(1:8,9,11:13) #adding together a specified sequence of values sum(seq(-3,7, by=2)) #creating two vector objects, and adding them together c1<-c(-1,1,2) •c2<-c(1,-3,8) •sum(c1,c2) #incorporating the sum function into a newly created vector total<-sum(1:8,9,11:13) •total #adding together vectors with mathematical operators sum(log(3), log(5)) ## the function sum can also be performed on a dataset. #In the following #example we will use the lynx #dataset. #suppose we want to know the total number of Canadian #lynx trappings from 1821-1934. #to see a list of available datasets and descriptions: data() #we can view the contents of the lynx dataset by #entering “lynx” lynx #finally, we can add together the values from a dataset #to return the total of number lynx trappings from #1821-1934: sum(lynx) #adding together sum(seq(-3, 7, by=2)) #the sum function can also be used to add together the #values from a column in a dataset. Just replace #“trees” and “Volume” with the name of your dataset and #the name of the column variable, respectively. #to view the contents of the trees dataset, enter #“trees” trees #to find the sum of tree volume sum(trees$Volume) #if empty values existed in the dataset, we would #enter: sum(trees$Volume, na.rm = TRUE) ## for complex vectors #use the function “complex” to create a complex vector, #for example: complex(real=stats::rnorm(50), imaginary = stats::rnorm(50)) #or create a new object, x in this case, by assigning #the complex vector to the static object x x<-complex(real=stats::rnorm(50), imaginary = stats::rnorm(50)) #sum the values of the complex vector by applying the #sum function to either the complex vector or the #static complex object sum((complex(real=stats::rnorm(50), imaginary = stats::rnorm(50)))) #or•sum(x) #note that complex vector object above is static, so #output is consistent, whereas generating the complex #vectors is dynamic and output is variable. ## for logical vectors #to create a logical vector, first combine some values #into a variable, in this case the variable y y <- c(1:12) •#y is now a variable composed of the values 1 through #12 #the following logical operators restrict value output #to values greater than 7 and less than 3 y[(y>7) | (y<3)] #sum the values of the logical vector by applying the #sum function to #the logical vector sum(y[(y>7) | (y<3)]) #end