英文:
How to retain class of variable in `tapply`?
问题
假设我的数据框设置如下:
X <- data.frame(
id = c('A', 'A', 'B', 'B'),
dt = as.Date(c('2020-01-01', '2020-01-02', '2021-01-01', '2021-01-02'))
)
我想要填充一个id特定的日期dt
最小值的变量。
使用:X$dtmin <- with(X, tapply(dt, id, min)[id])
返回一个数字,因为在tapply
中的simplify=T
将值转换为数字。为什么会这样呢?设置simplify=F
会返回一个列表,其中列表中的每个元素具有所需的数据结构,但将数据框X
中的变量填充回数字。然而,调用as.Date(<output>, origin='1970-01-01')
似乎显得多余。如何保留dt
的数据结构?
英文:
Suppose my data frame is set up like so:
X <- data.frame(
id = c('A', 'A', 'B', 'B'),
dt = as.Date(c('2020-01-01', '2020-01-02', '2021-01-01', '2021-01-02'))
)
and I want to populate a variable of the id-specific minimum value of date dt
Doing: X$dtmin <- with(X, tapply(dt, id, min)[id])
gives a numeric because the simplify=T
in tapply
has cast the value to numeric. Why has it done this? Setting simplify=F
returns a list which each element in the list has the desired data structure, but populating the variable in my dataframe X
casts these back to numeric. Yet calling as.Date(<output>, origin='1970-01-01')
seems needlessly verbose. How can I retain the data structure of dt
?
答案1
得分: 2
Sure, here is the translation of the provided code snippets:
我们可以使用
```R
X$dtmin <- with(X, do.call("c", tapply(dt, id, min, simplify = FALSE)[id]))
或者使用 dplyr
library(dplyr)
X %>%
mutate(dtmin = min(dt), .by = "id")
英文:
We may use
X$dtmin <- with(X, do.call("c", tapply(dt, id, min, simplify = FALSE)[id]))
Or use dplyr
library(dplyr)
X %>%
mutate(dtmin = min(dt), .by = "id")
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论