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.
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.
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.
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.
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.
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
Try lapply
instead with do.call
as in:
do.call(rbind, lapply(x, function(x) simpleFunc2(df=df, x)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With