在命名的小标题列表中子集和替换数据:如何获取索引,子集并保持列表格式和结构?

时间:2019-02-26 09:39:39

标签: r

我对向量,data.frames等更加熟悉,但是同意当一个函数具有多个输出时使用list会很方便。

我正在使用的程序包中的一个函数检索一个小标题的命名列表(不太熟悉小标题)或列表。

    List of 68
 $ 292684 :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    5 obs. of  3 variables:
  ..$ name: chr [1:5] "Animalia" "Arthropoda" "Chelicerata" "Arachnida" ...
  ..$ rank: chr [1:5] "Kingdom" "Phylum" "Subphylum" "Class" ...
  ..$ id  : int [1:5] 2 1065 1274 1300 292684
 $ 126752 :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    10 obs. of  3 variables:
  ..$ name: chr [1:10] "Animalia" "Chordata" "Vertebrata" "Gnathostomata" ...
  ..$ rank: chr [1:10] "Kingdom" "Phylum" "Subphylum" "Superclass" ...
  ..$ id  : int [1:10] 2 1821 146419 1828 10194 11014 151729 125516 125909 126752

我的问题是如何根据逻辑条件有效地导航此列表,子集,或者在维护列表格式的同时替换一些值?

我尝试过unlist()/ relist(),但是它没有保留结构。 我试过嵌套sapply sapply(sapply(mylist,“ [”,2),function(x)等等),然后将我的条件应用于输出以识别位置并使用eval(parse(paste ...)到达列表中的正确级别,但比较棘手...

 aa <- sapply(sapply(list.classif, "[", 2), length)
 who.max <- which(aa==max(aa))
 pos.list <- which(aa==max(aa))
 index <- names(who.max)
 best.classif <- eval(parse(text=paste("list.classif[pos.list]$`",index, 
 "`$rank", sep="") ))

如果我设法以这种方式对数据进行子集处理……似乎太复杂了,我确定有更好的解决方案了?

此外,如果要替换此特定位置的数据该怎么办? 你不能做经典的对象,不是吗?

object[index, index] <- replacebythat

可能的操作可能是:

  • 将“ Chordata”更改为...“任何”,
  • 将“类别”更改为...“某事”
  • 确定哪些生物具有“亚纲”“甲壳纲”(因此$ name ==甲壳纲)
  • 根据以上选择,更改信息。

带有dput()的数据集示例

    structure(list(`292684` = structure(list(name = c("Animalia", 
"Arthropoda", "Chelicerata", "Arachnida", "Acari"), rank = c("Kingdom", 
"Phylum", "Subphylum", "Class", "Subclass"), id = c(2L, 1065L, 
1274L, 1300L, 292684L)), .Names = c("name", "rank", "id"), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), `126752` = structure(list(
    name = c("Animalia", "Chordata", "Vertebrata", "Gnathostomata", 
    "Pisces", "Actinopterygii", "Perciformes", "Trachinoidei", 
    "Ammodytidae", "Ammodytes", "Ammodytes tobianus"), rank = c("Kingdom", 
    "Phylum", "Subphylum", "Superclass", "Superclass", "Class", 
    "Order", "Suborder", "Family", "Genus", "Species"), id = c(2L, 
    1821L, 146419L, 1828L, 11676L, 10194L, 11014L, 151729L, 125516L, 
    125909L, 126752L)), .Names = c("name", "rank", "id"), row.names = c(NA, 
-11L), class = c("tbl_df", "tbl", "data.frame")), `421139` = structure(list(
    name = c("Animalia", "Arthropoda", "Crustacea", "Multicrustacea", 
    "Hexanauplia", "Thecostraca", "Cirripedia", "Thoracica", 
    "Sessilia", "Balanomorpha", "Balanoidea", "Balanidae", "Amphibalaninae", 
    "Amphibalanus", "Amphibalanus improvisus"), rank = c("Kingdom", 
    "Phylum", "Subphylum", "Superclass", "Class", "Subclass", 
    "Infraclass", "Superorder", "Order", "Suborder", "Superfamily", 
    "Family", "Subfamily", "Genus", "Species"), id = c(2L, 1065L, 
    1066L, 845959L, 889925L, 22388L, 1082L, 1107L, 106033L, 106039L, 
    106041L, 106057L, 394026L, 415046L, 421139L)), .Names = c("name", 
"rank", "id"), row.names = c(NA, -15L), class = c("tbl_df", "tbl", 
"data.frame")), `1135` = structure(list(name = c("Animalia", 
"Arthropoda", "Crustacea", "Multicrustacea", "Malacostraca", 
"Eumalacostraca", "Peracarida", "Amphipoda"), rank = c("Kingdom", 
"Phylum", "Subphylum", "Superclass", "Class", "Subclass", "Superorder", 
"Order"), id = c(2L, 1065L, 1066L, 845959L, 1071L, 1086L, 1090L, 
1135L)), .Names = c("name", "rank", "id"), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame")), `103068` = structure(list(
    name = c("Animalia", "Arthropoda", "Crustacea", "Multicrustacea", 
    "Malacostraca", "Eumalacostraca", "Peracarida", "Amphipoda", 
    "Senticaudata", "Gammarida", "Gammaridira", "Gammaroidea", 
    "Bathyporeiidae", "Bathyporeia", "Bathyporeia pilosa"), rank = c("Kingdom", 
    "Phylum", "Subphylum", "Superclass", "Class", "Subclass", 
    "Superorder", "Order", "Suborder", "Infraorder", "Parvorder", 
    "Superfamily", "Family", "Genus", "Species"), id = c(2L, 
    1065L, 1066L, 845959L, 1071L, 1086L, 1090L, 1135L, 719424L, 
    236816L, 720166L, 720708L, 533675L, 101742L, 103068L)), .Names = c("name", 
"rank", "id"), row.names = c(NA, -15L), class = c("tbl_df", "tbl", 
"data.frame"))), .Names = c("292684", "126752", "421139", "1135", 
"103068"))

1 个答案:

答案 0 :(得分:0)

答案-将“ Chordata”更改为...“ Whatever”:

library(tidyverse)

lot %>%
  map(~ .x %>% mutate(name = if_else(name == 'Chordata', 'Whatever', name)))

答案-识别哪些生物具有“亚纲”“甲壳纲”(因此$ name ==甲壳纲):

lot %>%
  map(~ .x %>% filter(
    name == 'Crustacea'
  ))