Subsetting in R using OR condition with strings

后端 未结 2 1247
忘掉有多难
忘掉有多难 2021-02-04 14:04

I have a data frame with about 40 columns, the second column, data[2] contains the name of the company that the rest of the row data describes. However, the names of the compani

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 14:11

    First of all (as Jonathan done in his comment) to reference second column you should use either data[[2]] or data[,2]. But if you are using subset you could use column name: subset(data, CompanyName == ...).

    And for you question I will do one of:

    subset(data, data[[2]] %in% c("Company Name 09", "Company Name"), drop = TRUE) 
    subset(data, grepl("^Company Name", data[[2]]), drop = TRUE)
    

    In second I use grepl (introduced with R version 2.9) which return logical vector with TRUE for match.

提交回复
热议问题