Tod*_*son 6 r list subset dataframe dplyr
我有以下结构的数据集
\n\n site block treatment date insect1 insect2 insect3 insect4 ...\n1 location1 a chemical1 date1 0 0 10 1 \n2 location1 a chemical2 date1 1 0 2 0 \n3 location1 a chemical3 date1 0 0 23 1 \n4 location1 a chemical4 date1 0 0 5 0 \n5 location1 a chemical5 date1 0 0 9 0 \n6 location1 b chemical1 date1 0 1 5 0 \n7 location1 b chemical2 date1 1 0 5 1 \n8 location1 b chemical3 date1 0 0 4 0 \n9 location1 b chemical4 date1 0 0 5 0 \n10 location1 b chemical5 date1 3 0 12 0 \n11 location1 c chemical1 date1 0 0 2 1 \n12 location1 c chemical2 date1 0 0 0 0 \n13 location1 c chemical3 date1 0 0 4 0 \n14 location1 c chemical4 date1 0 0 2 7 \n15 location1 c chemical5 date1 2 0 5 0 \n16 location1 d chemical1 date1 0 0 8 1 \n17 location1 d chemical2 date1 0 0 3 0 \n18 location1 d chemical3 date1 0 0 10 0 \n19 location1 d chemical4 date1 0 0 2 0 \n20 location1 d chemical5 date1 0 1 7 0\n . . . . . . . . \n . . . . . . . . \n . . . . . . . .\n
Run Code Online (Sandbox Code Playgroud)\n\n该数据集是我进行的一项实验的结果,该实验测试了五种不同化学处理(化学品 1-5)对现场(地点 1)多种不同种类昆虫(此处为昆虫 1-4)的吸引力的影响。该实验在该现场的不同位置被阻止 (ad) 4 次,并在不同日期重复 5 次(仅显示日期 1)。所有这些信息都存储在数据集的前四列中。
\n\n接下来的一系列列(我有 46 列,但我只显示 4 列)表示不同种类的昆虫,以及我用特定化学物质捕获的昆虫数量在每个处理 x 块 x 日期组合内(=每行)。
\n\n作为分析的一部分,我想运行该数据集并找到我没有捕获昆虫的每种昆虫的块 x 日期的组合。例如,我在日期 1 的块 a 或 c 中没有捕获昆虫 2 的个体,因此我想将其从最终数据集中删除以进行分析。
\n\n我花了很多时间编写代码来完成这项任务,但昨晚我发现我的代码并没有像我想象的那样工作,我在试图弄清楚它时束手无策。这是到目前为止的代码(我已经包含了解决问题的所有步骤,这样人们就可以看到问题可能是在哪里引入的,或者建议更好的处理方法......):
\n\n创建一个列表,使每种昆虫(此处第 5-8 列)都有自己的数据框
\n\nsticky.list = lapply(sticky[-c(1:4,50)], function(i)data.frame(site=sticky$site, \n block=sticky$block,\n treatment=sticky$treatment,\n date=sticky$date,\n number=as.numeric(i)))\n
Run Code Online (Sandbox Code Playgroud)\n\n作为我的列表的一部分创建的数据帧之一的部分示例
\n\n$insect1\n site block treatment date number\n1 location1 a chemical1 date1 0\n2 location1 a chemical2 date1 1\n3 location1 a chemical3 date1 0\n4 location1 a chemical4 date1 0\n5 location1 a chemical5 date1 0\n
Run Code Online (Sandbox Code Playgroud)\n\n然后在列表中的每个数据帧中添加一个具有数据帧名称(即昆虫名称)的新列
\n\ntemp.list = Map(cbind, sticky.list, morphotype = names(sticky.list)) \n\n site block treatment date number morphotype\n1 location1 a chemical1 date1 0 insect1\n2 location1 a chemical2 date1 1 insect1 \n3 location1 a chemical3 date1 0 insect1\n4 location1 a chemical4 date1 0 insect1\n5 location1 a chemical5 date1 0 insect1\n
Run Code Online (Sandbox Code Playgroud)\n\n通过垂直组合来创建一个更大的数据集,然后展平每个列表元素(即,创建一个大数据帧。这会将我之前列表中的所有数据帧放在一个数据帧中。
\n\nsticky.list.combined.df <- temp.list %>% bind_rows(temp.list) %>% # make larger sample data\n mutate_if(is.list, simplify_all) %>% # flatten each list element internally \n unnest()\n
Run Code Online (Sandbox Code Playgroud)\n\n按块和形态类型进行分组,并根据该分组求出数字之和。然后,使用内部联接将此总和列添加到我们刚刚创建的主大型数据框(即 Sticky.list.combined.df)中。
\n\nsticky.list.combined.df.sum<- sticky.list.combined.df %>%\n group_by(date, block, morphotype) %>%\n summarize(sum = sum(number))\n\n# A tibble: 855 x 4\n# Groups: date, block [?]\n date block morphotype sum\n <fct> <fct> <chr> <dbl>\n 1 date1 a insect1 0\n 2 date1 a insect2 0\n 3 date1 a insect3 0\n 4 date1 a insect4 0\n# \xe2\x80\xa6 with 845 more rows\n
Run Code Online (Sandbox Code Playgroud)\n\n然后
\n\nsticky.list.analysis<-left_join(sticky.list.combined.df,sticky.list.combined.df.sum, by=c("date"="date",\n "morphotype"="morphotype")) \n
Run Code Online (Sandbox Code Playgroud)\n\n这是仅显示小昆虫 1 的输出示例。是否保留每个 block.x 的 5 行的决定因素是最后两列,block.y 和 sum,它表示每个块 (ad) 的化学品 1-5 捕获的所有昆虫的总和。
\n\n site block.x treatment date number morphotype block.y sum\n1 location1 a chemical1 date1 0 insect1 a 2\n2 location1 a chemical1 date1 0 insect1 b 8\n3 location1 a chemical1 date1 0 insect1 c 4\n4 location1 a chemical1 date1 0 insect1 d 0\n5 location1 a chemical2 date1 0 insect1 a 2\n6 location1 a chemical2 date1 0 insect1 b 8\n7 location1 a chemical2 date1 0 insect1 c 4\n8 location1 a chemical2 date1 0 insect1 d 0\n9 location1 a chemical3 date1 0 insect1 a 2\n10 location1 a chemical3 date1 0 insect1 b 8\n11 location1 a chemical3 date1 0 insect1 c 4\n12 location1 a chemical3 date1 0 insect1 d 0\n13 location1 a chemical4 date1 0 insect1 a 2\n14 location1 a chemical4 date1 0 insect1 b 8\n15 location1 a chemical4 date1 0 insect1 c 4\n16 location1 a chemical4 date1 0 insect1 d 0\n17 location1 a chemical5 date1 0 insect1 a 2\n18 location1 a chemical5 date1 0 insect1 b 8\n19 location1 a chemical5 date1 0 insect1 c 4\n20 location1 a chemical5 date1 0 insect1 d 0\n
Run Code Online (Sandbox Code Playgroud)\n\n我相信这就是我遇到的问题出现的地方
\n\n过滤总和 > 0 的行。
\n\n对于捕获日期(例如,date1)和形态类型的每个组合,删除该块中形态类型捕获量为零的行(即块ad)。在诱捕实验中(在汉克斯实验室统计实践中很常见),通常会删除或不包括没有捕获目标昆虫的日期。这可能与非生物因素(例如,太冷/太热、下雨)或与昆虫相关的物候因素有关。在数据中保留这些零会减少我们在数据中发现显着影响的机会,因此我们将排除它们。
\n\nsticky.list.analysis.reduced<- sticky.list.analysis %>% \n filter(sum > 0)\n
Run Code Online (Sandbox Code Playgroud)\n\n下面的缩短输出表明,对于insect1,我们应该保留块ac。保留哪些块将根据所观察的昆虫而有所不同。我现在想做的是从 block.y 获取这些数据并使用它来删除这些块的行。
\n\n不幸的是,这不是我想要的输出。R 根据总和列删除了一行。我们现在看到块 d 根据 block.y 列被删除。不幸的是我们需要删除第 46-60 行。
\n\n输出:
\n\n site block.x treatment date number morphotype block.y sum\n1 location1 a chemical1 date1 0 insect1 a 2\n2 location1 a chemical1 date1 0 insect1 b 8\n3 location1 a chemical1 date1 0 insect1 c 4\n4 location1 a chemical2 date1 0 insect1 a 2\n5 location1 a chemical2 date1 0 insect1 b 8\n6 location1 a chemical2 date1 0 insect1 c 4\n7 location1 a chemical3 date1 0 insect1 a 2\n8 location1 a chemical3 date1 0 insect1 b 8\n9 location1 a chemical3 date1 0 insect1 c 4\n10 location1 a chemical4 date1 0 insect1 a 2\n11 location1 a chemical4 date1 0 insect1 b 8\n12 location1 a chemical4 date1 0 insect1 c 4\n13 location1 a chemical5 date1 0 insect1 a 2\n14 location1 a chemical5 date1 0 insect1 b 8\n15 location1 a chemical5 date1 0 insect1 c 4\n16 location1 b chemical1 date1 0 insect1 a 2\n17 location1 b chemical1 date1 0 insect1 b 8\n18 location1 b chemical1 date1 0 insect1 c 4\n19 location1 b chemical2 date1 0 insect1 a 2\n20 location1 b chemical2 date1 0 insect1 b 8\n21 location1 b chemical2 date1 0 insect1 c 4\n22 location1 b chemical3 date1 0 insect1 a 2\n23 location1 b chemical3 date1 0 insect1 b 8\n24 location1 b chemical3 date1 0 insect1 c 4\n25 location1 b chemical4 date1 0 insect1 a 2\n26 location1 b chemical4 date1 0 insect1 b 8\n27 location1 b chemical4 date1 0 insect1 c 4\n28 location1 b chemical5 date1 0 insect1 a 2\n29 location1 b chemical5 date1 0 insect1 b 8\n30 location1 b chemical5 date1 0 insect1 c 4\n31 location1 c chemical1 date1 0 insect1 a 2\n32 location1 c chemical1 date1 0 insect1 b 8\n33 location1 c chemical1 date1 0 insect1 c 4\n34 location1 c chemical2 date1 0 insect1 a 2\n35 location1 c chemical2 date1 0 insect1 b 8\n36 location1 c chemical2 date1 0 insect1 c 4\n37 location1 c chemical3 date1 0 insect1 a 2\n38 location1 c chemical3 date1 0 insect1 b 8\n39 location1 c chemical3 date1 0 insect1 c 4\n40 location1 c chemical4 date1 0 insect1 a 2\n41 location1 c chemical4 date1 0 insect1 b 8\n42 location1 c chemical4 date1 0 insect1 c 4\n43 location1 c chemical5 date1 0 insect1 a 2\n44 location1 c chemical5 date1 0 insect1 b 8\n45 location1 c chemical5 date1 0 insect1 c 4\n46 location1 d chemical1 date1 0 insect1 a 2\n47 location1 d chemical1 date1 0 insect1 b 8\n48 location1 d chemical1 date1 0 insect1 c 4\n49 location1 d chemical2 date1 0 insect1 a 2\n50 location1 d chemical2 date1 0 insect1 b 8\n51 location1 d chemical2 date1 0 insect1 c 4\n52 location1 d chemical3 date1 0 insect1 a 2\n53 location1 d chemical3 date1 0 insect1 b 8\n54 location1 d chemical3 date1 0 insect1 c 4\n55 location1 d chemical4 date1 0 insect1 a 2\n56 location1 d chemical4 date1 0 insect1 b 8\n57 location1 d chemical4 date1 0 insect1 c 4\n58 location1 d chemical5 date1 0 insect1 a 2\n59 location1 d chemical5 date1 0 insect1 b 8\n60 location1 d chemical5 date1 0 insect1 c 4\n
Run Code Online (Sandbox Code Playgroud)\n\n期望的输出:
\n\n site block.x treatment date number morphotype block.y sum\n1 location1 a chemical1 date1 0 insect1 a 2\n2 location1 a chemical1 date1 0 insect1 b 8\n3 location1 a chemical1 date1 0 insect1 c 4\n4 location1 a chemical2 date1 0 insect1 a 2\n5 location1 a chemical2 date1 0 insect1 b 8\n6 location1 a chemical2 date1 0 insect1 c 4\n7 location1 a chemical3 date1 0 insect1 a 2\n8 location1 a chemical3 date1 0 insect1 b 8\n9 location1 a chemical3 date1 0 insect1 c 4\n10 location1 a chemical4 date1 0 insect1 a 2\n11 location1 a chemical4 date1 0 insect1 b 8\n12 location1 a chemical4 date1 0 insect1 c 4\n13 location1 a chemical5 date1 0 insect1 a 2\n14 location1 a chemical5 date1 0 insect1 b 8\n15 location1 a chemical5 date1 0 insect1 c 4\n16 location1 b chemical1 date1 0 insect1 a 2\n17 location1 b chemical1 date1 0 insect1 b 8\n18 location1 b chemical1 date1 0 insect1 c 4\n19 location1 b chemical2 date1 0 insect1 a 2\n20 location1 b chemical2 date1 0 insect1 b 8\n21 location1 b chemical2 date1 0 insect1 c 4\n22 location1 b chemical3 date1 0 insect1 a 2\n23 location1 b chemical3 date1 0 insect1 b 8\n24 location1 b chemical3 date1 0 insect1 c 4\n25 location1 b chemical4 date1 0 insect1 a 2\n26 location1 b chemical4 date1 0 insect1 b 8\n27 location1 b chemical4 date1 0 insect1 c 4\n28 location1 b chemical5 date1 0 insect1 a 2\n29 location1 b chemical5 date1 0 insect1 b 8\n30 location1 b chemical5 date1 0 insect1 c 4\n31 location1 c chemical1 date1 0 insect1 a 2\n32 location1 c chemical1 date1 0 insect1 b 8\n33 location1 c chemical1 date1 0 insect1 c 4\n34 location1 c chemical2 date1 0 insect1 a 2\n35 location1 c chemical2 date1 0 insect1 b 8\n36 location1 c chemical2 date1 0 insect1 c 4\n37 location1 c chemical3 date1 0 insect1 a 2\n38 location1 c chemical3 date1 0 insect1 b 8\n39 location1 c chemical3 date1 0 insect1 c 4\n40 location1 c chemical4 date1 0 insect1 a 2\n41 location1 c chemical4 date1 0 insect1 b 8\n42 location1 c chemical4 date1 0 insect1 c 4\n43 location1 c chemical5 date1 0 insect1 a 2\n44 location1 c chemical5 date1 0 insect1 b 8\n45 location1 c chemical5 date1 0 insect1 c 4\n
Run Code Online (Sandbox Code Playgroud)\n\n一旦这个问题得到解决,我想从其列中对每个昆虫进行子集化(我知道如何手动执行此操作,但不是针对所有昆虫物种,但这是一个完全不同的问题),然后运行广义线性混合模型来评估处理对捕获每只昆虫的影响,日期和地点作为随机影响。
\n\n我很感激对此事的任何见解。如果我需要编辑此内容以添加任何其他信息,请告诉我,我已尽力使数据和问题的结构变得清晰。谢谢。
\n你试过这个subset
功能吗?它是在base
R 包下定义的(链接)。
您可以执行以下操作:
filtered.sticky.list.analysis <- subset(sticky.list.analysis, block.x == "a" || block.x == "b" || block.x == "c")
Run Code Online (Sandbox Code Playgroud)
另一件可行的事情是:
filtered.sticky.list.analysis <- subset(sticky.list.analysis, block.x != "d")
Run Code Online (Sandbox Code Playgroud)
代码非常清楚。第一个选项选择block.x
等于a
、b
或 的所有内容c
。第二个选项选择与 不同的所有内容d
。
归档时间: |
|
查看次数: |
942 次 |
最近记录: |