Programing

논리적 조건으로 data.frame 행 필터링

lottogame 2020. 6. 18. 07:55
반응형

논리적 조건으로 data.frame 행 필터링


data.frame논리적 조건을 기반으로 행을 필터링하고 싶습니다 . 내가 같은 데이터 프레임을 가지고 있다고 가정 해 봅시다

   expr_value     cell_type
1    5.345618 bj fibroblast
2    5.195871 bj fibroblast
3    5.247274 bj fibroblast
4    5.929771          hesc
5    5.873096          hesc
6    5.665857          hesc
7    6.791656          hips
8    7.133673          hips
9    7.574058          hips
10   7.208041          hips
11   7.402100          hips
12   7.167792          hips
13   7.156971          hips
14   7.197543          hips
15   7.035404          hips
16   7.269474          hips
17   6.715059          hips
18   7.434339          hips
19   6.997586          hips
20   7.619770          hips
21   7.490749          hips

내가 원하는 것은 동일하게 보이지만 하나의 cell_type에 대한 데이터 만있는 새로운 데이터 프레임을 얻는 것입니다. 예를 들어 셀 유형 "hesc"가 포함 된 하위 집합 / 선택 행 :

   expr_value     cell_type
1    5.929771          hesc
2    5.873096          hesc
3    5.665857          hesc

또는 셀 유형 "bj fibroblast"또는 "hesc":

   expr_value     cell_type
1    5.345618 bj fibroblast
2    5.195871 bj fibroblast
3    5.247274 bj fibroblast
4    5.929771          hesc
5    5.873096          hesc
6    5.665857          hesc

이 작업을 수행하는 쉬운 방법이 있습니까?

난 노력 했어:

expr[expr[2] == 'hesc']
# [1] "5.929771" "5.873096" "5.665857" "hesc"     "hesc"     "hesc"    

원래 데이터 프레임을 "expr"이라고하지만 결과에서 볼 수 있듯이 잘못된 형식으로 표시됩니다.


하나의 'cell_type'(예 : 'hesc') 에 따라 행을 선택하려면 ==다음을 사용하십시오 .

expr[expr$cell_type == "hesc", ]

둘 이상의 다른 'cell_type'(예 : 'hesc' 또는 'bj fibroblast') 에 따라 행을 선택하려면 %in%다음을 사용하십시오 .

expr[expr$cell_type %in% c("hesc", "bj fibroblast"), ]

사용 subset(대화식 사용)

subset(expr, cell_type == "hesc")
subset(expr, cell_type %in% c("bj fibroblast", "hesc"))

또는 더 나은 dplyr::filter()

filter(expr, cell_type %in% c("bj fibroblast", "hesc"))

expr[expr[2] == 'hesc']작동하지 않는 이유 는 데이터 프레임의 경우 x[y]행이 아닌 열을 선택하기 때문입니다. 행을 선택하려면 x[y,]대신 구문으로 변경하십시오 .

> expr[expr[2] == 'hesc',]
  expr_value cell_type
4   5.929771      hesc
5   5.873096      hesc
6   5.665857      hesc

dplyr패키지를 사용할 수 있습니다 :

library(dplyr)
filter(expr, cell_type == "hesc")
filter(expr, cell_type == "hesc" | cell_type == "bj fibroblast")

때로는 필터링하려는 열이 열 인덱스 2와 다른 위치에 나타나거나 변수 이름이있을 수 있습니다.

이 경우 필터링하려는 열 이름간단히 참조 할 수 있습니다 .

columnNameToFilter = "cell_type"
expr[expr[[columnNameToFilter]] == "hesc", ]

나는 데이터 프레임에서 작업하고 제공된 답변으로 운이 없었으며 항상 0 행을 반환하므로 grepl을 찾아서 사용했습니다.

df = df[grepl("downlink",df$Transmit.direction),]

Which basically trimmed my dataframe to only the rows that contained "downlink" in the Transmit direction column. P.S. If anyone can guess as to why I'm not seeing the expected behavior, please leave a comment.

Specifically to the original question:

expr[grepl("hesc",expr$cell_type),]

expr[grepl("bj fibroblast|hesc",expr$cell_type),]

No one seems to have included the which function. It can also prove useful for filtering.

expr[which(expr$cell == 'hesc'),]

This will also handle NAs and drop them from the resulting dataframe.

Running this on a 9840 by 24 dataframe 50000 times, it seems like the which method has a 60% faster run time than the %in% method.


we can use data.table library

  library(data.table)
  expr <- data.table(expr)
  expr[cell_type == "hesc"]
  expr[cell_type %in% c("hesc","fibroblast")]

or filter using %like% operator for pattern matching

 expr[cell_type %like% "hesc"|cell_type %like% "fibroblast"]

참고URL : https://stackoverflow.com/questions/1686569/filter-data-frame-rows-by-a-logical-condition

반응형