Suppose I have a data frame with columns c1, …, cn, and a function f that takes in the columns of this data frame as arguments.
How can I apply f to each row of the data frame to get a new data frame?
For example,
x = data.frame(letter=c('a','b','c'), number=c(1,2,3))
# x is
# letter | number
# a | 1
# b | 2
# c | 3
f = function(letter, number) { paste(letter, number, sep='') }
# desired output is
# a1
# b2
# c3
How do I do this? I’m guessing it’s something along the lines of {s,l,t}apply(x, f), but I can’t figure it out.
as @greg points out, paste() can do this. I suspect your example is a simplification of a more general problem. After struggling with this in the past, as illustrated in this previous question, I ended up using the plyr package for this type of thing. plyr does a LOT more, but for these things it’s easy:
you’ll want to rename the output columns, I’m sure
So while I was typing this, @joshua showed an alternative method using
ddply. The difference in my example is thatadplytreats the input data frame as an array.adplydoes not use the “group by” variablerowthat @joshua created. How he did it is exactly how I was doing it until Hadley tipped me to theadply()approach. In the aforementioned question.