Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a function to each data frame

Tags:

loops

r

I have 4 data frames which contain a date column, a price column and a return column.

data.1:

Date        Price  Return
2009-01-02  100    0.2
2009-01-03  110    0.1
etc.

data.2:

Date        Price  Return
2009-02-02  60    0.15
2009-02-03  50    -0.1
etc.

I would like to set up a loop and apply the function density() to each data frame, returning the density values for the returns.

I through about creating a list, setting up a loop and using lapply() to do this, so

> ff <- list(data.1, data.2, data.3, data.4)
> for(i in 1:length(ff){
        density[[i]] <- lapply(ff, density(ff[[i]]$Return))}

but this obviously doesn't work. Could somebody offer me some help?

Thanks in advance - Dani

like image 626
Dani Avatar asked Jan 25 '11 09:01

Dani


People also ask

How do you apply a function to each element of a DataFrame?

Apply Function to Every Row of DataFrameBy using apply() you call a function to every row of pandas DataFrame. Here the add() function will be applied to every row of pandas DataFrame. In order to iterate row by row in apply() function use axis=1 .

How do I apply a function to a DataFrame in R?

In R Programming Language to apply a function to every integer type value in a data frame, we can use lapply function from dplyr package. And if the datatype of values is string then we can use paste() with lapply.

What is apply () in pandas?

The apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.


2 Answers

First, you should initialize density if you want to do that manual assignment.

densities <- list()

Second, you use the density function in a funny way. You should specify the function different in your lapply. Either you give the function and the extra arguments after the comma, or you construct your own custom little function in the lapply call, as shown below.

data.1 <- data.frame(
    X1 = letters[1:10],
    X2 = 1:10
)

data.2 <- data.frame(
    X1 = letters[11:20],
    X2 = 10:1
)

ff <- list(data.1,data.2)

densities <- lapply(ff,function(i) {density(i$X2)})

This returns a list automatically.

To get the data out of it, you simply use the list indices:

densities[[1]]$x

If you named your list before, you could use the names as well :

names(ff) <- c("data.1","data.2")

densities <- lapply(ff,function(i) {density(i$X2)})
densities[['data.1']]$x
like image 95
Joris Meys Avatar answered Oct 22 '22 05:10

Joris Meys


The thing with lapply is that you don't need to use a for-loop. This should work:

data.1=data.2=data.3=data.4=matrix(rnorm(30),ncol=3)

ff=list(data.1,data.2,data.3,data.4)

densities=lapply(ff,function(x)density(x[,3]))

Although there is undoubtedly a better way to do this (I mean the manual assignment of the list).

like image 40
Sacha Epskamp Avatar answered Oct 22 '22 04:10

Sacha Epskamp