【发布时间】:2019-11-28 06:25:43
【问题描述】:
我有一个包含 16 个变量的 1401 个观察值的数据框。对于每一列(第一列除外),我有 1(如果满足条件)或 0(如果不满足条件)。 我根据所有条件(所有列)以不同的顺序(例如,第 1 列,然后第 2 列,然后第 3 列等或第 3 列,然后第 12 列,然后第 1 列)对我的数据进行子集化。为了比较不同的场景,我绘制了子集的每一步之后的观察次数。
我编写了我的代码,它可以工作,但它非常混乱且冗长,它肯定会从你的建议中受益。
dput(droplevels(head(data,20)))
structure(list(Substance = structure(c(13L, 9L, 10L, 12L, 1L,
19L, 16L, 17L, 5L, 2L, 14L, 7L, 4L, 6L, 20L, 18L, 15L, 3L, 11L,
8L), .Label = c("104653-34-1", "107-02-8", "111-30-8", "12057-74-8",
"122454-29-9", "14915-37-8", "20859-73-8", "27083-27-8", "28772-56-7",
"3691-35-8", "55965-84-9", "56073-07-5", "56073-10-0", "5836-29-3",
"71751-41-2", "74-90-8", "81-81-2", "86347-14-0", "90035-08-8",
"91465-08-6"), class = "factor"), colA = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
colB = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L), colC = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), colD = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L), colE = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
1L, 1L), colF = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), colG = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
1L), colH = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), colI = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L
), colK = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L,
0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L), colJ = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L,
0L, 0L), colL = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L,
0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L), colM = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), colN = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), colO = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("Substance",
"Oral", "Dermal", "Inhalation", "SC", "SED", "RS", "SS", "M",
"C", "R", "STOT.SE", "STOT.RE", "AT", "Eco.Acute", "Eco.Chronic"
), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 12L, 13L,
14L, 17L, 18L, 19L, 20L, 21L, 22L, 28L, 34L), class = "data.frame")
#scenario A
#I count the number of observations for each condition
count_0 <- count(data)
count_1 <- sum(data$colA == 1)
count_2 <- sum(data$colA == 1 & data$colB == 1)
count_3 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1)
count_4 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1)
count_5 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1& data$colD == 1 & data$colE == 1)
count_6 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1)
count_7 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1& data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1)
count_8 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1 & data$colH == 1)
count_9 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1& data$colH == 1 & data$colI == 1)
count_10 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1& data$colH == 1 & data$colI == 1 & data$colJ == 1)
count_11 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1 & data$colH == 1 & data$colI == 1 & data$colJ == 1 & data$colK == 1)
count_12 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1 & data$colH == 1 & data$colI == 1 & data$colJ == 1 & data$colK == 1& data$colL == 1)
count_13 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1& data$colH == 1 & data$colI == 1& data$colJ == 1 & data$colK == 1 & data$colL == 1 & data$colM == 1)
count_14 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1 & data$colD == 1 & data$colE == 1 & data$colF == 1 & data$colG == 1 & data$colH == 1 & data$colI == 1 & data$colJ == 1 & data$colK == 1 & data$colL == 1 & data$colM == 1 & data$colN == 1)
count_15 <- sum(data$colA == 1 & data$colB == 1 & data$colC == 1& data$colD == 1 & data$colE == 1& data$colF == 1 & data$colG == 1 & data$colH == 1 & data$colI == 1 & data$colJ == 1 & data$colK == 1 & data$colL == 1 & data$colM == 1 & data$colN == 1 & data$colO == 1)
scenarioA <- rbind(count_0,count_1,count_2,count_3,count_4,count_5,count_6,count_7,count_8,count_9,count_10,count_11,count_12,count_13, count_14,count_15)
#I add a column to the new dataframe "scenarioA" to indicate that the results correspond to scenario A (for later use for ggplot)
scenario <- c("scenarioA","scenarioA","scenarioA","scenarioA","scenarioA","scenarioA","scenarioA","scenarioA","scenarioA", "scenarioA","scenarioA","scenarioA","scenarioA","scenarioA","scenarioA","scenarioA")
scenarioA <- cbind(rownames(scenarioA),scenarioA, scenario)
rownames(scenarioA) <- NULL
colnames(scenarioA) <- c("endpoint","hits","scenario")
#I repeat the same for scenario B but with different order in the subsetting
count_1 <- sum(data$colM == 1)
count_2 <- sum(data$colM == 1 & data$colC == 1)
count_3 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1)
count_4 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1)
count_5 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1)
count_6 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1)
count_7 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1)
count_8 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1)
count_9 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1)
count_10 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1 & data$colG == 1)
count_11 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1 & data$colG == 1 & data$colB == 1)
count_12 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1 & data$colG == 1 & data$colB == 1& data$colJ == 1)
count_13 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1 & data$colG == 1 & data$colB == 1& data$colJ == 1 & data$colL == 1)
count_14 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1 & data$colG == 1 & data$colB == 1& data$colJ == 1 & data$colL == 1 & data$colN == 1)
count_15 <- sum(data$colM == 1 & data$colC == 1 & data$colE == 1 & data$colH == 1 & data$colF == 1 & data$colA == 1 & data$colD == 1 & data$colO == 1 & data$colI == 1 & data$colG == 1 & data$colB == 1& data$colJ == 1 & data$colL == 1 & data$colN == 1 & data$colK == 1)
#I create a new data frame with the results from the scenario B
scenarioB <- rbind(count_0, count_1,count_2,count_3,count_4,count_5,count_6,count_7, count_8,count_9,count_10,count_11,count_12,count_13, count_14,count_15)
scenario <- c("scenarioB","scenarioB","scenarioB","scenarioB","scenarioB","scenarioB","scenarioB","scenarioB","scenarioB", "scenarioB","scenarioB","scenarioB","scenarioB","scenarioB","scenarioB","scenarioB")
scenarioB <- cbind(rownames(scenarioB),scenarioB, scenario)
rownames(scenarioB) <- NULL
colnames(scenarioB) <- c("endpoint","hits","scenario")
#I create a new data frame with scenarios A & B
scenarios <- as.data.frame(rbind(scenarioA, scenarioB))
scenarios$endpoint <- as.character(c("count_0", "count_1","count_2","count_3","count_4","count_5","count_6","count_7",
"count_8","count_9","count_10","count_11","count_12","count_13","count_14","count_15"))
scenarios$endpoint <- factor(scenarios$endpoint, levels=unique(scenarios$endpoint))
scenarios$hits <- as.numeric(as.character(scenarios$hits))
#I plot the results in one single graph to compare the 2 scenarios
library(ggplot2)
ggplot(scenarios, aes(x=endpoint, y=hits, color=scenario))+
geom_point()+
theme_minimal()+
theme(axis.text.x = element_text(hjust=.5))
代码运行但并不简单。最终我将使用超过 2 个场景,所以我想找到一种更好的方法来编写这个。我想到了循环和/或嵌套。
【问题讨论】:
-
嗯,你肯定有一个错误,因为当我输入你的代码时,我得到
Error in group_vars(x) : object 'data' not found。是不是你忘了给我们一些minimal example data 来工作? :-) -
我用我的数据添加了指向 Google 表格的链接。希望没事。
-
请将您的数据添加为
dput,并包含当前和预期的输出。使用dput(head(df,n))。根据需要选择n。此外,如此多的 sum 调用使它看起来过于复杂,也许有一种更简单的方法。没有你的数据很难说。 将数据共享为 dput 对象而不是链接
标签: r loops optimization nested subset