I am attempting to apply a custom function that calls components of that dataframe to do a calculation. I have made a trivial example below because my actual problem is very hard to make a reproducible example. In the below example I want to have the first two columns be added together to create a third column which is the sum of them. Below is an example I found online that gets close to what I want:
celebrities=data.frame(name=c("Andrew","matt","Dany","Philip","John","bing","Monica"),
age=c(28,23,49,29,38,23,29),
income=c(25.2,10.5,11,21.9,44,11.5,45))
f=function(x,output){
name=x[1]
income=x[3]
cat(name,income,"\n")
}
apply(celebrities,1,f)
But when I try to take it and apply mathematical function it doesn't work:
f2=function(x,output){
age=x[2]
income=x[3]
sum(age,income)
}
apply(celebrities,1,f2)
In essence what I need is for apply to take a dataset, go through every row of that dataset using the values in that row as inputs into the function and add a third column to the dataset with the results of the function. Please let me know how I can clarify this question if needed. I have referred to the questions below, but they don't seem to work for me.
Apply a function to every row of a matrix or a data frame
How to assign new values from lapply to new column in dataframes in list
Call apply-like function on each row of dataframe with multiple arguments from each row
apply
on adata.frame
, it is converted to amatrix
for the processing. If any of the columns (of the processed frame) arecharacter
, the all columns are converted tocharacter
, defeating any math operations. Though I tend to discourageapply
with frames, if you must then make sure that you only use a portion of it, something likeapply(celebrities[c("age","income")], 1, sum)
. - r2evanslibrary(plyr)
such asadply
oraaply
(depending on what you want the output format to be like) which don't coerce all columns tocharacter
- Sarahdplyr
now has arowwise
function that can help you do what you're looking for. E.g.,library(dplyr) ; celebrities %>% rowwise %>% mutate(new_var = f(var1, var2))
- Jake Fisher