这是我的数据
disease <- c("high", "high", "high", "high", "low","low","low","low");
ToA <- c("P","A","P","P","A","A","A","P");
ToB <- c("P","A","A","P","A","P","A","P");
ToC <- c("P","P","A","P","A","A","A","P");
df <- data.frame(disease, ToA, ToB, ToC)我正在寻找应急表作为图像,其中,第1行= ToA,第2行= ToB,第3行= ToC
high_P high_A low_P low_A
1 3 1 1 3
2 2 2 2 2
3 2 1 1 3对于数据中的每一列,我需要计算P& high (疾病)、P、above和above组合的频率(计数),就像你在上面的图像中看到的那样。我可以对每一栏分别按nrow方式进行如下操作:
df中用于二次谐波的##count
high_P=nrow(df[df$disease=="high" & df$ToA=="P", ])
high_A=nrow(df[df$disease=="high" & df$ToA=="A", ])
low_P=nrow(df[df$disease=="low" & df$ToA=="P", ])
low_A=nrow(df[df$disease=="low" & df$ToA=="A", ])
ToA_df=data.frame(high_P,high_A,low_P,low_A)#在df中计算col 3
high_P=nrow(df[df$disease=="high" & df$ToB=="P", ])
high_A=nrow(df[df$disease=="high" & df$ToB=="A", ])
low_P=nrow(df[df$disease=="low" & df$ToB=="P", ])
low_A=nrow(df[df$disease=="low" & df$toB=="A", ])
ToB_df=data.frame(high_P,high_A,low_P,low_A)#计算df中的col 4
high_P=nrow(df[df$disease=="high" & df$ToC=="P", ])
high_A=nrow(df[df$disease=="high" & df$ToC=="A", ])
low_P=nrow(df[df$disease=="low" & df$ToC=="P", ])
low_A=nrow(df[df$disease=="low" & df$ToC=="A", ])
ToC_df=data.frame(high_P,high_A,low_P,low_A)
Data = rbind(ToA_df,ToB_df,ToC_df)它可以做我想做的事情,但是我想用循环来计算每一列,对于一个大数据集来说,手工计算是很困难的。有人能建议/帮助我如何使用图像中的循环or....as来计算R中的应急表吗?
发布于 2022-09-18 15:32:39
您的数据有一个p,而不是一个P,然后使用以下代码:
使用reshape::recast
reshape2::recast(df, variable~disease+value, id.var = 'disease')
variable high_A high_P low_A low_P
1 ToA 1 3 3 1
2 ToB 2 2 2 2
3 ToC 1 3 3 1使用tidyverse
df %>%
pivot_longer(-disease, values_transform = toupper)%>%
pivot_wider(name, names_from = c(disease, value),
values_from = disease, values_fn = length)
# A tibble: 3 x 5
name high_P high_A low_A low_P
<chr> <int> <int> <int> <int>
1 ToA 3 1 3 1
2 ToB 2 2 2 2
3 ToC 3 1 3 1R基选项:
table(rev(stack(Map(\(x,y)paste(y,x,sep='_'), df[-1], df[1]))))
values
ind high_A high_P low_A low_P
ToA 1 3 3 1
ToB 2 2 2 2
ToC 1 3 3 1发布于 2022-09-18 15:32:28
你可以这样做:
library(dplyr)
library(tidyr)
df %>%
pivot_longer(!disease, names_to = 'columns', values_to = 'vals') %>%
count(disease, columns, vals) %>%
pivot_wider(names_from = c(disease, vals), values_from = n,
names_sep = '_')
# A tibble: 3 × 5
columns high_A high_P low_A low_P
<chr> <int> <int> <int> <int>
1 ToA 1 3 3 1
2 ToB 2 2 2 2
3 ToC 1 3 3 1发布于 2022-09-18 15:40:55
将table by病循环到列上。
by(df[-1], df$disease, \(x) t(sapply(x, table))) |> do.call(what=cbind)
# A P A P
# ToA 1 3 3 1
# ToB 2 2 2 2
# ToC 1 3 3 1数据:
df <- structure(list(disease = c("high", "high", "high", "high", "low",
"low", "low", "low"), ToA = c("P", "A", "P", "P", "A", "A", "A",
"P"), ToB = c("P", "A", "A", "P", "A", "P", "A", "P"), ToC = c("P",
"P", "A", "P", "A", "A", "A", "P")), row.names = c(NA, -8L), class = "data.frame")https://stackoverflow.com/questions/73763590
复制相似问题