Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply function returning data.frames with factors to sequence

Tags:

dataframe

r

How do I apply a function which returns a data.frame with factors to a sequence?

Example:

s <- factor(c(10, 20, 30))
t <- factor(c("a", "b", "a"))
v <- c(5, 6, 4)

df <- data.frame(s,t,v)

So the data.frame df is this:

   s t v
1 10 a 5
2 20 b 6
3 30 a 4

I also have a function which returns a data.frame:

simpleFunc2 <- function(df, x){
  tmp <- subset(df, df$s == x)
  return(tmp)
}

Now I have a sequence

x <- c(20, 30, 10, 30, 10)

and want to the result auf applying the function simpleFunc2 to this sequence.

I use sapply

sapply(x, function(x) simpleFunc2(df, x))

But I get

  [,1]     [,2]     [,3]     [,4]     [,5]    
s factor,1 factor,1 factor,1 factor,1 factor,1
t factor,1 factor,1 factor,1 factor,1 factor,1
v 6        4        5        4        5  

How do I get the right values of the factors back?

This example is simplified. So maybe there's a much simpler way to do it in this case.

like image 977
JerryWho Avatar asked Jan 06 '13 19:01

JerryWho


People also ask

How do I apply a function to multiple data frames in R?

To join more than two (multiple) R data frames use the reduce() function from tidyverse package. This function takes all the data frames as a list and joins the data frames based on the specified column.

What is data frames and explain applying functions to data frames?

Data Frames are data displayed in a format as a table. Data Frames can have different types of data inside it. While the first column can be character , the second and third can be numeric or logical . However, each column should have the same type of data.

What does apply () do in R?

The apply() function lets us apply a function to the rows or columns of a matrix or data frame. This function takes matrix or data frame as an argument along with function and whether it has to be applied by row or column and returns the result in the form of a vector or array or list of values obtained.

How do I apply the same function to all rows and columns of a matrix in R?

One of the most famous and most used features of R is the *apply() family of functions, such as apply() , tapply() , and lapply() . Here, we'll look at apply() , which instructs R to call a user-specified function on each of the rows or each of the columns of a matrix.


2 Answers

I see you have gotten an answer to your question, but I think your approach to selecting the superset from that dataframe was too involved. (And my apologies if that function was not representative. I'd like to offer a method of extraction that is faster than going through subset:

> df[ match(x, df$s), ]
     s t v
2   20 b 6
3   30 a 4
1   10 a 5
3.1 30 a 4
1.1 10 a 5
# Save results as from:
> do.call(rbind, lapply(x, function(x) simpleFunc2(df, x)) )
    s t v
2  20 b 6
3  30 a 4
31 10 a 5
32 30 a 4
5  10 a 5
like image 134
IRTFM Avatar answered Dec 16 '22 00:12

IRTFM


Try lapply instead with do.call as in:

do.call(rbind, lapply(x, function(x) simpleFunc2(df=df, x)))
like image 20
Tyler Rinker Avatar answered Dec 16 '22 01:12

Tyler Rinker