0

Say that I start with a dataframe. I want to perform a function on each row. The function will take multiple columns of the row as arguments. I want the end result to be a list of dataframes, where each dataframe corresponds to a row of the original dataframe. How can I do this? I am getting confused with the various apply functions and some other functions in the tidyverse.

Here is an example:

I start with this dataframe:

a <- data.frame(a= c(1,2), b=c(2,3), c=c(0,0))

I would like to perform a function that simply adds columns a, b, and c. The output is the result, where each row's sum is contained in a dataframe. The name of the dataframe should correspond to the row number.

b <- list(r1=data.frame(3), r2=data.frame(5))

1 Answer 1

1

You can try to first create your now variable with mutate, then split your data.frame with a row_number index.

library(dplyr)

a %>% mutate(sum_of_rows=rowSums(.)) %>%
      split(1:nrow(a)) %>%
      setNames(paste0('r', 1:nrow(a))

That will work if you want a whole row of the data.frame for every element of the list.

If you just want a list of data.frames with a single element each, as in your example, you can make it simply with:

rowSums(a) %>%
as.data.frame() %>%
split(1:nrow(.)) %>%
setNames(paste0('r', 1:nrow(a))

Or with just some base R:

setNames(split(as.data.frame(rowSums(a)), 1:nrow(a)), paste0('r', 1:nrow(a))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.