首页 > 解决方案 > 如何将这个包含 4 个物种名称的数据框分成 4 个数据框,每个数据框只包含一个物种?

问题描述

我需要分离这些数据,以便我可以制作图表(ggplot),并将它们叠加到我正在从事的项目中,但我正在努力将大数据框分成 4 个基于物种名称的较小的 data.frames!谢谢!

我使用了 split 功能,但它没有达到我想要的最终目标。

# A tibble: 230 x 18
   `Bee ID` Species Site    Treatment Wax   WaxTime Egg    EggTime Survival
      <int> <chr>   <chr>   <chr>     <chr>   <int> <chr>    <int> <chr>   
 1      264 lucorum Heather Control   wax        13 NA          21 survived
 2      267 lucorum Heather Control   nowax      29 NA          25 survived
 3      291 lucorum Heather Control   wax         9 noeggs      29 survived
 4      312 lucorum Heather Control   nowax      29 noeggs      29 survived
 5      315 lucorum Heather Control   nowax      29 NA          16 survived
 6      324 lucorum Heather Control   wax         9 noeggs      29 survived
 7      327 lucorum Heather Control   nowax      29 eggs        26 survived
 8      330 lucorum Heather Control   nowax      29 eggs        24 survived
 9      390 lucorum Heather Control   nowax      29 noeggs      29 died    
10      393 lucorum Heather Control   nowax      29 eggs        11 survived

标签: rdataframeparsing

解决方案


就像评论中所说的那样,你没有给我们很多信息。但是,我想我知道你在找什么。我不知道你的其他物种叫什么,所以我有几个占位符Species2,Species3Species4. 您需要填写这些将您的物种名称。

让我们轻松调用您的数据框data,但这必须由您调用的数据替换。

Lucorum_data <- data[data$Species=="lucorum",]
Species2_data <- data[data$Species=="Species2",]
Species3_data <- data[data$Species=="Species3",]
Species4_data <- data[data$Species=="Species4",]

此代码将抓取 Species=="yourspeciesname" 的所有行,并将其分配给我称为“yourspeciesname_data”的新对象。


推荐阅读