Reputation: 1284
I have a dataframe with a numerical variable (Acc
) and four categorical variables (ID
,Datetime
, Period
and 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
I am trying to estimate mean Acc
per ID
, Day
, Period
and State
. To do so, I am trying to apply this code:
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)
I should get this:
#> `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
However, I get the message Error in map_lgl(.x, .p, ...) : object 'State' not found
.
I don't understand where is the mistake. I guess it is something silly, but I have been hours thinking on that and nothing.
Does someone shed light on why I get an error message?
Upvotes: 0
Views: 2129
Reputation: 27
I encountered a similar error with my names_from =
and values_from =
variables and was able to get it to work by putting those variable names in quotes. I can't reproduce your error unfortunately so this seems to be a recent change.
Upvotes: 0
Reputation: 11584
Does this work:
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
>
Upvotes: 3
Reputation: 521
I'm not sure where in your code the error was coming from but I think it was from the final select
line. Instead of selecting anything that wasn't State.NA_NA I switched it to keep everything column that didn't contain an NA in it. This seems to get the desired output.
Also looking at the comments I would recommend calling each tidyverse package with the syntax dplyr::select
as you may be unintentionally calling a different 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
Created on 2020-11-11 by the reprex package (v0.3.0)
Upvotes: 1