Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

summary - repeat group analysis on multiple columns in R

I have trouble finding the right code for the following problem:

My data table has multiple columns representing different variables representing repeated measurements. I would like to summarize the repeated measurements in R

enter image description here

library(data.table)
data.table(structure(list(exp = c("480 p36", "480 p36", "480 p36", "480 p36", 
"480 p36", "480 p36"), well = c("A", "A", "A", "A", "A", "A"), 
    col = c("001", "002", "003", "004", "005", "006"), `T peak` = c(1.63, 
    1.67, 1.56, 1.47, 1.59, 1.55), `T rise` = c(0.2, 0.18, 0.18, 
    0.13, 0.14, 0.17), `T fall` = c(0.13, 0.16, 0.16, 0.15, 0.23, 
    0.2), TCT = c(0.86, 0.77, 0.73, 0.76, 0.71, 0.62), `D peak` = c(0.02, 
    0.02, 0.02, 0.02, 0.01, 0.01), `D valley` = c(0, 0, 0, 0, 
    0, 0), `D high` = c(0.01, 0.02, 0.02, 0.02, 0.01, 0.01), 
    `D low` = c(0, 0, 0, 0, 0, 0), AUC = c(0.01, 0.01, 0.01, 
    0.01, 0, 0), Power = c(0.01, 0.01, 0.01, 0.01, 0, 0), CR = c(0.1, 
    0.12, 0.16, 0.14, 0.07, 0.05), RR = c(-0.1, -0.11, -0.11, 
    -0.12, -0.04, -0.04)), row.names = c(NA, -6L), class = "data.frame"))

In a first step I grouped the table with

group <- group_by(output, col)

I made a variable vector:

variables <- colnames(group[,4:15], do.NULL = TRUE)

I would like to summarize each variable grouped by column col and column exp. Tried to summarize the data like mean, SEM, and count.

it works with

summarize(group, mTCT = mean(`TCT`, na.rm = TRUE), SEM_TCT = sd(`TCT`,na.rm=TRUE)/sqrt(n()), nTCT=n())

But I thought it would be better to replace every single column name with the variable vector. But that doesn't work. Any advice?

question from:https://stackoverflow.com/questions/65873995/repeat-group-analysis-on-multiple-columns-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hi Welcome to stackoverflow.

Tried the following code.

data <- data.table(structure(list(exp = c("480 p36", "480 p36", "480 p36", "480 p36", 
"480 p36", "480 p36"), well = c("A", "A", "A", "A", "A", "A"), 
    col = c("001", "002", "003", "004", "005", "006"), `T peak` = c(1.63, 
    1.67, 1.56, 1.47, 1.59, 1.55), `T rise` = c(0.2, 0.18, 0.18, 
    0.13, 0.14, 0.17), `T fall` = c(0.13, 0.16, 0.16, 0.15, 0.23, 
    0.2), TCT = c(0.86, 0.77, 0.73, 0.76, 0.71, 0.62), `D peak` = c(0.02, 
    0.02, 0.02, 0.02, 0.01, 0.01), `D valley` = c(0, 0, 0, 0, 
    0, 0), `D high` = c(0.01, 0.02, 0.02, 0.02, 0.01, 0.01), 
    `D low` = c(0, 0, 0, 0, 0, 0), AUC = c(0.01, 0.01, 0.01, 
    0.01, 0, 0), Power = c(0.01, 0.01, 0.01, 0.01, 0, 0), CR = c(0.1, 
    0.12, 0.16, 0.14, 0.07, 0.05), RR = c(-0.1, -0.11, -0.11, 
    -0.12, -0.04, -0.04)), row.names = c(NA, -6L), class = "data.frame"))



data %>%
    janitor::clean_names() %>%
    tidyr::pivot_longer( # Tranform Wide -> Long
        cols = t_peak:rr,
        names_to = "variables"
    ) %>% 
    group_by(exp, col, variables) %>%
    summarise(
        mean = mean(value, na.rm = TRUE),
        sd = sd(value, na.rm = TRUE) / sqrt(n())
    )  %>%
    tidyr::pivot_wider( # Return to "normal" state
        names_from = variables, 
        values_from = c(mean, sd)
    )

Hope this can help for the task.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...