我有一个带有数字变量(Acc
)和四个类别变量(ID
,Datetime
,Period
和State
的数据框。
# My data
df <- data.frame(ID=c(rep(c("A"),8),rep(c("B"),8)),
Datetime=c("2020-08-05 12:00:00","2020-08-05 17:00:00","2020-08-05 18:03:00","2020-08-05 22:54:00","2020-08-06 01:08:00","2020-08-06 13:26:00","2020-08-06 19:04:00","2020-08-08 11:00:00",
"2020-08-04 03:00:00","2020-08-04 15:00:00","2020-08-04 23:00:00","2020-08-06 14:00:00","2020-08-06 17:00:00","2020-08-06 20:00:00","2020-08-07 04:00:00","2020-08-07 16:00:00"),
Period=c("Day","Day","Day","Night","Night","Day","Night","Day","Night","Day","Night","Day","Day","Night","Night","Day"),
State=c(1,2,1,1,1,1,2,2,1,1,1,2,2,1,1,1),
Acc=c(1.1,2.3,1.7,1.4,0.1,1.9,2.9,2.3,1.1,0.1,1.4,0.2,2.6,1.3,1.7,1.0))
df$Datetime <- as.POSIXct(df$Datetime,format="%Y-%m-%d %H:%M:%S", tz="UTC")
df
ID Datetime Period State Acc
1 A 2020-08-05 12:00:00 Day 1 1.1
2 A 2020-08-05 17:00:00 Day 2 2.3
3 A 2020-08-05 18:03:00 Day 1 1.7
4 A 2020-08-05 22:54:00 Night 1 1.4
5 A 2020-08-06 01:08:00 Night 1 0.1
6 A 2020-08-06 13:26:00 Day 1 1.9
7 A 2020-08-06 19:04:00 Night 2 2.9
8 A 2020-08-08 11:00:00 Day 2 2.3
9 B 2020-08-04 03:00:00 Night 1 1.1
10 B 2020-08-04 15:00:00 Day 1 0.1
11 B 2020-08-04 23:00:00 Night 1 1.4
12 B 2020-08-06 14:00:00 Day 2 0.2
13 B 2020-08-06 17:00:00 Day 2 2.6
14 B 2020-08-06 20:00:00 Night 1 1.3
15 B 2020-08-07 04:00:00 Night 1 1.7
16 B 2020-08-07 16:00:00 Day 1 1.0
我正在尝试估算每个Acc
,ID
,Day
和Period
的平均值State
。为此,我正在尝试应用以下代码:
library(tidyverse)
df %>%
group_by(ID, Day = as.factor(as.Date(Datetime)), Period, State, .drop = FALSE) %>%
summarise(Acc = mean(Acc, na.rm = TRUE)) %>%
pivot_wider(names_from = c(State, Period),
values_from = Acc,
names_prefix = "State.") %>%
select(!State.NA_NA)
我应该得到这个:
#> `summarise()` regrouping output by 'ID', 'Day', 'Period' (override with `.groups` argument)
#> # A tibble: 10 x 6
#> # Groups: ID, Day [10]
#> ID Day State.1_Day State.2_Day State.1_Night State.2_Night
#> <chr> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 A 2020-08-04 NA NA NA NA
#> 2 A 2020-08-05 1.4 2.3 1.4 NA
#> 3 A 2020-08-06 1.9 NA 0.1 2.9
#> 4 A 2020-08-07 NA NA NA NA
#> 5 A 2020-08-08 NA 2.3 NA NA
#> 6 B 2020-08-04 0.1 NA 1.25 NA
#> 7 B 2020-08-05 NA NA NA NA
#> 8 B 2020-08-06 NA 1.4 1.3 NA
#> 9 B 2020-08-07 1 NA 1.7 NA
#> 10 B 2020-08-08 NA NA NA NA
但是,我收到消息Error in map_lgl(.x, .p, ...) : object 'State' not found
。
我不知道哪里出了错。我想这有点愚蠢,但是我已经花了好几个小时思考,但一无所获。
有人了解我为什么收到错误消息吗?
答案 0 :(得分:3)
这项工作:
df %>%
group_by(ID, Day = as.factor(as.Date(Datetime)), Period, State, .drop = FALSE) %>%
summarise(Acc = mean(Acc, na.rm = TRUE)) %>% na.omit() %>%
pivot_wider(names_from = c(State, Period),
values_from = Acc,
names_prefix = "State.") %>% complete(ID, nesting(Day)) %>% arrange(ID, Day) %>% distinct()
`summarise()` regrouping output by 'ID', 'Day', 'Period' (override with `.groups` argument)
# A tibble: 10 x 6
# Groups: ID, Day [10]
ID Day State.1_Day State.2_Day State.1_Night State.2_Night
<fct> <fct> <dbl> <dbl> <dbl> <dbl>
1 A 2020-08-04 NA NA NA NA
2 A 2020-08-05 1.4 2.3 1.4 NA
3 A 2020-08-06 1.9 NA 0.1 2.9
4 A 2020-08-07 NA NA NA NA
5 A 2020-08-08 NA 2.3 NA NA
6 B 2020-08-04 0.1 NA 1.25 NA
7 B 2020-08-05 NA NA NA NA
8 B 2020-08-06 NA 1.4 1.3 NA
9 B 2020-08-07 1 NA 1.7 NA
10 B 2020-08-08 NA NA NA NA
>
答案 1 :(得分:1)
我不确定错误是从哪里来的,但我认为是来自最后select
行。而不是选择不是State.NA_NA的任何内容,而是将其切换为保留其中不包含NA的所有列。这似乎获得了所需的输出。
也请查看注释,我建议使用语法dplyr::select
调用每个tidyverse程序包,因为您可能无意中调用了不同的select
。
library(tidyverse)
df <- data.frame(ID=c(rep(c("A"),8),rep(c("B"),8)),
Datetime=c("2020-08-05 12:00:00","2020-08-05 17:00:00","2020-08-05 18:03:00","2020-08-05 22:54:00","2020-08-06 01:08:00","2020-08-06 13:26:00","2020-08-06 19:04:00","2020-08-08 11:00:00",
"2020-08-04 03:00:00","2020-08-04 15:00:00","2020-08-04 23:00:00","2020-08-06 14:00:00","2020-08-06 17:00:00","2020-08-06 20:00:00","2020-08-07 04:00:00","2020-08-07 16:00:00"),
Period=c("Day","Day","Day","Night","Night","Day","Night","Day","Night","Day","Night","Day","Day","Night","Night","Day"),
State=c(1,2,1,1,1,1,2,2,1,1,1,2,2,1,1,1),
Acc=c(1.1,2.3,1.7,1.4,0.1,1.9,2.9,2.3,1.1,0.1,1.4,0.2,2.6,1.3,1.7,1.0))
df$Datetime <- as.POSIXct(df$Datetime,format="%Y-%m-%d %H:%M:%S", tz="UTC")
df
#> ID Datetime Period State Acc
#> 1 A 2020-08-05 12:00:00 Day 1 1.1
#> 2 A 2020-08-05 17:00:00 Day 2 2.3
#> 3 A 2020-08-05 18:03:00 Day 1 1.7
#> 4 A 2020-08-05 22:54:00 Night 1 1.4
#> 5 A 2020-08-06 01:08:00 Night 1 0.1
#> 6 A 2020-08-06 13:26:00 Day 1 1.9
#> 7 A 2020-08-06 19:04:00 Night 2 2.9
#> 8 A 2020-08-08 11:00:00 Day 2 2.3
#> 9 B 2020-08-04 03:00:00 Night 1 1.1
#> 10 B 2020-08-04 15:00:00 Day 1 0.1
#> 11 B 2020-08-04 23:00:00 Night 1 1.4
#> 12 B 2020-08-06 14:00:00 Day 2 0.2
#> 13 B 2020-08-06 17:00:00 Day 2 2.6
#> 14 B 2020-08-06 20:00:00 Night 1 1.3
#> 15 B 2020-08-07 04:00:00 Night 1 1.7
#> 16 B 2020-08-07 16:00:00 Day 1 1.0
df %>%
dplyr::group_by(ID, Day = as.factor(as.Date(Datetime)), Period, State, .drop = FALSE) %>%
dplyr::summarise(Acc = mean(Acc, na.rm = TRUE)) %>%
tidyr::pivot_wider(names_from = c(State, Period),
values_from = Acc,
names_prefix = "State.") %>%
dplyr::select(!contains("NA"))
#> `summarise()` regrouping output by 'ID', 'Day', 'Period' (override with `.groups` argument)
#> # A tibble: 10 x 6
#> # Groups: ID, Day [10]
#> ID Day State.1_Day State.2_Day State.1_Night State.2_Night
#> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 A 2020-08-04 NA NA NA NA
#> 2 A 2020-08-05 1.4 2.3 1.4 NA
#> 3 A 2020-08-06 1.9 NA 0.1 2.9
#> 4 A 2020-08-07 NA NA NA NA
#> 5 A 2020-08-08 NA 2.3 NA NA
#> 6 B 2020-08-04 0.1 NA 1.25 NA
#> 7 B 2020-08-05 NA NA NA NA
#> 8 B 2020-08-06 NA 1.4 1.3 NA
#> 9 B 2020-08-07 1 NA 1.7 NA
#> 10 B 2020-08-08 NA NA NA NA
由reprex package(v0.3.0)于2020-11-11创建