如何根据来自 2 个以上其他数据帧的条件填充数据帧中的空列,所有这些数据帧都具有不同的长度?

  • 本文关键字:数据帧 所有这些 填充 何根 条件 其他 r
  • 更新时间 :
  • 英文 :


我有第一个数据帧(名为"fish_12"),有 74610 行,每行都有海洋鱼类标本的数据。第一列是每个标本所属物种的名称(整个数据帧中有许多属于同一物种的标本),第二列 BIN 是每个物种的一种 ID 号,然后我有每个标本的收集者姓名、采集国家和我要填充的空列等级。

species        |    BIN      |    collectors  |  country      | grade
--------------------------------------------------------------------------
Tilapia guineensis  |BOLD:AAL5979 |    C.D. Nwani  |     Nigeria   | NA
Tilapia zillii      |BOLD:AAB9042 |    C.D. Nwani  |     Nigeria   | NA
Fundulus rubrifrons |BOLD:AAI7245 |  John Donavan  |  United States| NA
Eutrigla gurnardus  |BOLD:AAC0262 |Hermann Neumann |    North Sea  | NA
Sprattus sprattus   |BOLD:AAE9187 |Hermann Neumann |    North Sea  | NA
Gadus morhua        |BOLD:ACF1143 |Hermann Neumann |    North Sea  | NA
Tilapia zillii      |BOLD:AAB9042 |     C.D. Nwani |      Nigeria  | NA
Gadus morhua        |BOLD:ACF1169 |   Angela Cicia |  United States| NA

往下看,基本上一个物种只能有一个或多个 BIN,并且同一个 BIN 偶尔可以分配给不同的物种。

所以我要做的是填充将等级"E"分配给分配给 BIN 的每个物种的列,而 BIN 本身分配给 1 个以上的不同物种; 在第一个数据帧中出现次数少于 3 次的每个物种的等级"D";"C"表示分配了1个以上不同BIN的物种,但同时分配给该特定物种的每个BIN仅分配给一个物种;"B"表示仅分配给一个BIN的物种,但其每个标本都是从同一收集者和同一国家收集的;最后是"A",表示每个只分配了一个BIN但从多个不同收集者或多个国家收集标本的物种。

因此,我所做的是创建一个新的数据帧,其中包括一列,其中包含分配给每个物种的BIN数量(bin_per_species);另一个带有一列的列显示每个BIN编号(species_per_bin)存在多少物种;另一个带有一列显示每个物种存在多少收集器(collectors_per_species);最后有一个列,显示每个物种分配了多少个国家(country_per_species)

#creating the other dataframe from the first one 
fish_13=fish_12%>% 
group_by(species) %>%
summarise(occurrence = n_distinct(BIN),
BIN = str_c(unique(BIN), collapse = ","))
names(fish_13)=c("species","bin_per_species","BIN")
View(fish_13)
fish_14=fish_12%>% 
group_by(BIN) %>%
summarise(occurrence = n_distinct(species),
species = str_c(unique(species), collapse = ","))
names(fish_14)=c("BIN","species_per_bin","species")
View(fish_14)
length(unique(fish_14$BIN))
fish_15=fish_12%>% 
group_by(species) %>%
summarise(occurrence = n_distinct(collectors),
collectors = str_c(unique(collectors), collapse = ","))
names(fish_15)=c("species","collector_per_species","collectors")
View(fish_15)
fish_16=fish_12%>% 
group_by(species) %>%
summarise(occurrence = n_distinct(country),
country = str_c(unique(country), collapse = ","))
names(fish_16)=c("species","countries_per_species","country")
View(fish_16)

因此,从这里开始,我尝试使用各种if/else函数来形成条件,但我遇到的问题是数据帧具有不同的长度,并且我无法同时分配从A到E的所有等级,因为即使我设法没有错误,其中一些也会转换回NA。我想要的输出基本上是第一个数据帧,每个样本都有一个等级。

抱歉,如果我感到困惑并以错误的方式呈现数据,但我是这个社区的新手,我正在努力变得更好。提前感谢您的任何回复

首先,欢迎来到 SO。

现在关于你的问题:我在试图理解所有规则时感到有点困惑,但我认为解决方案可能很容易。

您有这些规则主要基于 BIN 行,迭代这些值并从数据中执行子集,然后应用函数来检查规则并更新成绩。

喜欢这个:

bins = unique(fish_12$BIN)
for(b in bins) {
# Get the index so you can update only the grade of the subset
sub_fish_index = which(fish_12$BIN == b)
sub_fish_data = fish_12[,sub_fish_index]
# use a function to identiffy the patterns and apply the rules (return a vector of rules)
new_grade = apply_rules(sub_fish_data)
# Update grade in the main data.frame
fish_12$grade[sub_fish_index] = new_grade
}

我返回了一个成绩向量,因为某些规则可能能够使用此信息并设置正确的成绩。

我希望这对你有所帮助。

相关内容

最新更新