Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To compute the sum of a range within a data frame by assigning the column names to a variable in R, you can use the following syntax:

# assign column names to a variable
cols <- c("column1", "column2", "column3")

# compute sum of a range using the variable
sum(df[, cols[1]:cols[3]])

In this example, cols is a vector containing the names of the columns you want to sum. The df[, cols[1]:cols[3]] expression selects the columns from column1 to column3 in the data frame df, and the sum() function computes their sum.