I've read many questions related to mine, however I can't figure out what's wrong with my code
The package I use is "dplyr" & "infotheo"
Usage of infotheo here is condentropy(time2, time1)
my data is like
id <- c("1", "1", "1", "1", "2", "2", "2", "2", "3", "3", "3", "3")
cond <- c("1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2")
time1 <- c("1", "3", "3", "2", "3", "3", "1", "1", "1", "2", "2", "1")
time2 <- c("3", "3", "2", "3", "3", "1", "1", "1", "2", "2", "1" ,"1")
df <- data.frame(id, cond, time1, time2)
I want to calculate it by id & condition, which means I'll get 6 entropy values from 3 person with two conditions. Here is my code
df %>%
group_by(df$id, df$cond) %>%
summarize(condentropy(df$time2, df$time1))
why I only got one value for all the groups?
Thanks for the help in advance!
Something like this
First, convert you data to numics
df <- df %>% type_convert()
-- Column specification ------------------------------------------
cols(
id = col_double(),
cond = col_double(),
time1 = col_double(),
time2 = col_double()
)
Second, get at finding relevant means,
df %>%
group_by(id, cond) %>%
summarise(mean = mean(id))
`summarise()` has grouped output by 'id'. You can override using the `.groups` argument.
# A tibble: 6 x 3
# Groups: id [3]
id cond mean
<dbl> <dbl> <dbl>
1 1 1 1
2 1 2 1
3 2 1 2
4 2 2 2
5 3 1 3
6 3 2 3
Third, study this page for addition examples.
Convert the time columns to numeric, perform the grouping and summarize. Do not use df$ with dplyr verbs and be sure to assign the value of condentropy(...) to a column name. The subject of the question refers to mean but the code suggests you want to calculate the conditional entropy so we provide both.
library(dplyr)
library(infotheo)
df %>%
mutate(time1 = as.numeric(time1), time2 = as.numeric(time2)) %>%
group_by(id, cond) %>%
summarize(cond_ent = condentropy(time2, time1),
mean1 = mean(time1), mean2 = mean(time2), .groups = "drop")
Use type.convert(as.is = TRUE)
to get numeric variables and then summarise
with across
: You don't have to use $
This one:
library(dplyr)
library(infotheo)
df %>%
as_tibble() %>%
type.convert(as.is=TRUE) %>%
group_by(id, cond) %>%
summarise(mean = mean(c(time1, time2)))
Output:
id cond mean
<int> <int> <dbl>
1 1 1 2.25
2 1 2 2.75
3 2 1 2
4 2 2 1.5
5 3 1 1.5
6 3 2 1.5
OR
library(dplyr)
df %>%
as_tibble() %>%
type.convert(as.is=TRUE) %>%
group_by(id, cond) %>%
summarise(across(starts_with("time"), mean))
Output:
id cond time1 time2
<int> <int> <dbl> <dbl>
1 1 1 2 2.5
2 1 2 2.5 3
3 2 1 2 2
4 2 2 2 1
5 3 1 1.5 1.5
6 3 2 1.5 1.5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.