如何在`tapply`中保留变量的类别?

huangapple go评论64阅读模式
英文:

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 &lt;- data.frame(
  id = c(&#39;A&#39;, &#39;A&#39;, &#39;B&#39;, &#39;B&#39;),
  dt = as.Date(c(&#39;2020-01-01&#39;, &#39;2020-01-02&#39;, &#39;2021-01-01&#39;, &#39;2021-01-02&#39;))
)

and I want to populate a variable of the id-specific minimum value of date dt

Doing: X$dtmin &lt;- 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(&lt;output&gt;, origin=&#39;1970-01-01&#39;) 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 &lt;- with(X, do.call(&quot;c&quot;, tapply(dt, id, min, simplify = FALSE)[id]))

或者使用 dplyr

library(dplyr)
X %&gt;%
   mutate(dtmin = min(dt), .by = &quot;id&quot;)
英文:

We may use

X$dtmin &lt;- with(X, do.call(&quot;c&quot;, tapply(dt, id, min, simplify = FALSE)[id]))

Or use dplyr

library(dplyr)
X %&gt;%
   mutate(dtmin = min(dt), .by = &quot;id&quot;)


</details>



huangapple
  • 本文由 发表于 2023年2月18日 03:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定