asahi
asahi

Reputation: 3333

A basic apply anonymous function

I have a dataframe a that has columns id and date and a second dataframe b that has id as its first column. For each row in b, I'm trying to find all rows in a with the same id, and then find the minimum of the dates. I'm using the code below, but when I run this, I'm getting a numeric as opposed to dates. I'm wondering if someone can help me with this.

class(a$date)
# "Date"
funP <- function(x){
    b <- subset(a, id==x[1])
    min(b$date)
}
f <- apply(b, 1, funP)
class(f)
# "numeric"

Upvotes: 2

Views: 129

Answers (1)

lgbi
lgbi

Reputation: 221

Apparently the apply function converts date values. The manual (?apply) mentions:

Value:

[...] In all cases the result is coerced by ‘as.vector’ to one of the basic vector types [...]

You could convert it back to the Date class:

f <- as.Date(f, origin="1970-01-01")

Upvotes: 2

Related Questions