bill999
bill999

Reputation: 2529

How to run function on each row of a dataframe, while using multiple arguments from that dataframe, and outputting new dataframes in a list

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))

Upvotes: 0

Views: 134

Answers (1)

GuedesBF
GuedesBF

Reputation: 9868

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))

Upvotes: 1

Related Questions