I'm new to R, and I'm trying to write a function that will add the entries of a data frame column by row, and return the data frame with
- a column of the new row of sums
- that column named.
Here's a sample df of my data:
Ethnicity <- c('A', 'B', 'H', 'N', 'O', 'W', 'Unknown')
Texas <- c(2,41,56,1,3,89,7)
Tenn <- c(1,9,2,NA,1,32,3)
When I directly try the following code, the columns are summed by row as desired:
new_df <- df %>% rowwise() %>%
mutate(TN_TX = sum(Tenn, Texas, na.rm = TRUE))
But when I try to use my function code, rowwise() seems not to work. My function code is:
df.sum.col <- function(df.in, col.1, col.2) {
if(is.data.frame(df.in) != TRUE){ #warning if first arg not df
warning('df.in is not a dataframe')}
if(is.numeric(col.1) != TRUE){
warning('col.1 is not a numeric vector')}
if(is.numeric(col.2) != TRUE){
warning('col.2 is not a numeric vector')} #warning if col not numeric
df.out <- rowwise(df.in) %>%
mutate(name = sum(col.1, col.2, na.rm = TRUE))
df.out
}
bad_df <- df.sum(df,Texas, Tenn)
This results in
.
I don't understand why the core of the function works outside it but not within. I also tried piping df.in to rowsum() like this:
f.out <- df.in %>% rowwise() %>%
mutate(name = sum(col.1, col.2, na.rm = TRUE))
But that doesn't resolve the problem.
As far as naming the new column, I tried doing so by adding the name as an argument, but didn't have any success. Thoughts on this?
Any help appreciated!