요소 변수 (사과 또는 바나나 중 하나)가있는 일부 데이터가 있는데 값이 하나 인 데이터 세트의 위치를 식별 할 수 있기를 원합니다 두 개의 연속 된 행에있는이 두 옵션 중 하나 (즉, 사과는 행 4 & 5, 바나나는 아래 행 8 &). 중복 된 함수가 여기서 유용 할 것입니다 (즉, Index out the subsequent row with an identical value in R). 그러나 나는 원하는 변수를 가진 원하는 출력을 얻는 방법을 잘 모릅니다.다음 행에 동일한 문자 값이있는 데이터 프레임의 행을 결정하십시오. R
예 데이터 :
test = structure(list(cnt = c(87L, 51L, 24L, 69L, 210L, 21L, 15L, 9L,
12L), type = c("apple", "banana", "apple", "banana", "banana",
"apple", "banana", "apple", "apple")), .Names = c("cnt", "type"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-9L), spec = structure(list(cols = structure(list(cnt = structure(list(), class = c("collector_integer",
"collector")), type = structure(list(), class = c("collector_character",
"collector"))), .Names = c("cnt", "type")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
원하는 출력 : 나는 다음과 같은 코드를 사용하면
cnt type output
1 87 apple FALSE
2 51 banana FALSE
3 24 apple FALSE
4 69 banana TRUE
5 210 banana TRUE
6 21 apple FALSE
7 15 banana FALSE
8 9 apple TRUE
9 12 apple TRUE
난 그냥 사과와 바나나 모두가 중복되는 것을! :
나에게 말한다 요약을 얻을test[!duplicated(test[,"type], fromLast=TRUE,]
도움을 주시면 감사하겠습니다.
시험 유형 % mutate (출력 = (유형 == 리드 (유형, 기본값 = 유형 [n() - 1]) | 유형 == 지연 (type, default = type [2]))' – akrun