首页 文章

一列的子集因子

提问于
浏览
1

我有数据集(b.data),其中包含调查中记录的海洋物种信息 . 每行与下面的列 Headers 的单个动物有关:

“Area”“Year”“Cruise”“Vessel”“Haul”“Haul_ID”“Common_name”“Scientific_Name”“Length_mm”“Sex”“Width”

我想将数据子集化为仅包括某些物种进行分析 . 我写了下面的代码,命名我要包括的物种(31种)

species.list <- c("Blonde ray","Brown crab","Cod","Common dab", ... etc

我不知道如何编写代码然后从整个数据集中对这些行进行子集化 . 我已经尝试了下面的代码,但它返回0观察 .

z=b.data[rownames(b.data$Common_name) %in% species.list,]

2 回答

  • 1

    你只需要摆脱 row.names() . 考虑:

    b.data <- data.frame(Common_name=c("Blonde ray","Brown crab","Cod",
                                       "Common dab", LETTERS[1:7]), 
                         x=rnorm(11))
    b.data
    #    Common_name          x
    # 1   Blonde ray  0.3631655
    # 2   Brown crab -0.6668250
    # 3          Cod -0.2829071
    # 4   Common dab  0.3893549
    # 5            A  0.7206768
    # 6            B -0.9790288
    # 7            C -0.5366370
    # 8            D  1.9940040
    # 9            E  1.2800009
    # 10           F -0.2024495
    # 11           G -2.1230087
    

    这是 row.names() 返回的内容:

    row.names(b.data)
    # [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11"
    row.names(b.data$Common_name)
    #
    

    对于 b.data ,它是行号,因为我没有分配任何行名,而 b.data$Common_name 只是数据框中的变量,它根本没有任何行名 . 如果你离开 row.names() ,这就是你得到的:

    species.list <- c("Blonde ray","Brown crab","Cod","Common dab")
    z = b.data[b.data$Common_name %in% species.list,]
    z
    #   Common_name          x
    # 1  Blonde ray  0.3631655
    # 2  Brown crab -0.6668250
    # 3         Cod -0.2829071
    # 4  Common dab  0.3893549
    
  • 2

    几乎在那里我想 - 怎么样

    z=b.data[b.data$Common_name %in% species.list,]
    

相关问题