Ask Your Question
2

In R, what is the method of computing the sum of a range within a data frame by assigning the column names to a variable?

asked 2023-06-19 01:42:49 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-19 02:01:02 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 01:42:49 +0000

Seen: 10 times

Last updated: Jun 19 '23