每次将另一列的文本更改r恢复为0

发布于 2025-02-13 01:22:28 字数 622 浏览 0 评论 0原文

在R中,我有一个带有一个时间列的数据框,每当另一列中的文本更改时,我希望将其恢复为一个或零。文本再也没有回到上一段文字。 文本更改的395倍,〜270'000行。

目前

时间试验
11A
12A
13B
14B

表想要

时间试验
1A
2A
2 A 1B
2B

In R, I have a data frame with a time column that I want to revert to one or zero every time the text in another column changes. The text never goes back to the previous text.
There are 395 times the text changes and ~270'000 rows.

Table at the moment

TimeTrial
11A
12A
13B
14B

Table wanted

TimeTrial
1A
2A
1B
2B

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2025-02-20 01:22:28

1)基本解决方案是:

transform(DF, Time = ave(Time, Trial, FUN = seq_along))

2)另一个基本解决方案是:

transform(DF, Time = 1:nrow(DF) - match(Trial, Trial) + 1)

3)dplyr 我们可以写:

library(dplyr)
DF %>%
  group_by(Trial) %>%
  mutate(Time = 1:n()) %>%
  ungroup

基准测试

基准基础解决方案问题的数据速度要快得多,但建议您使用数据或其中一部分重复此操作。

library(dplyr)
library(microbenchmark)

microbenchmark(
  base1 = transform(DF, Time = ave(Time, Trial, FUN = seq_along)),
  base2 = transform(DF, Time = 1:nrow(DF) - match(Trial, Trial) + 1),
  dplyr = DF %>% group_by(Trial) %>% mutate(Time = 1:n()) %>% ungroup
)
## Unit: microseconds
##   expr     min      lq      mean   median       uq      max neval cld
##  base1   555.8   578.5   654.702   626.95   692.95   1345.7   100  a 
##  base2   308.5   330.2   415.610   391.75   410.30    950.6   100  a 
##  dplyr 11076.4 11354.9 13846.736 11543.80 11751.65 101861.9   100   b

注意

以可重复形式输入。

DF <- structure(list(Time = 11:14, Trial = c("A", "A", "B", "B")), 
  class = "data.frame", row.names = c(NA, -4L))

1) A base solution would be:

transform(DF, Time = ave(Time, Trial, FUN = seq_along))

2) Another base solution would be:

transform(DF, Time = 1:nrow(DF) - match(Trial, Trial) + 1)

3) dplyr With dplyr we can write:

library(dplyr)
DF %>%
  group_by(Trial) %>%
  mutate(Time = 1:n()) %>%
  ungroup

Benchmark

The base solutions are much faster on the data in the question but suggest you repeat this with your data or a subset of it.

library(dplyr)
library(microbenchmark)

microbenchmark(
  base1 = transform(DF, Time = ave(Time, Trial, FUN = seq_along)),
  base2 = transform(DF, Time = 1:nrow(DF) - match(Trial, Trial) + 1),
  dplyr = DF %>% group_by(Trial) %>% mutate(Time = 1:n()) %>% ungroup
)
## Unit: microseconds
##   expr     min      lq      mean   median       uq      max neval cld
##  base1   555.8   578.5   654.702   626.95   692.95   1345.7   100  a 
##  base2   308.5   330.2   415.610   391.75   410.30    950.6   100  a 
##  dplyr 11076.4 11354.9 13846.736 11543.80 11751.65 101861.9   100   b

Note

Input in reproducible form.

DF <- structure(list(Time = 11:14, Trial = c("A", "A", "B", "B")), 
  class = "data.frame", row.names = c(NA, -4L))
怀中猫帐中妖 2025-02-20 01:22:28

我们可以分组,然后使用row_number()

library(dplyr)

df %>% 
  group_by(Trial) %>% 
  mutate(Time = row_number()) %>%
  ungroup()
   Time Trial
  <int> <chr>
1     1 A    
2     2 A    
3     1 B    
4     2 B  

We could group and then use row_number()

library(dplyr)

df %>% 
  group_by(Trial) %>% 
  mutate(Time = row_number()) %>%
  ungroup()
   Time Trial
  <int> <chr>
1     1 A    
2     2 A    
3     1 B    
4     2 B  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文