2017-11-10 9 views
0

데이터 프레임은 1209 열 및 27900 행입니다.R 데이터 프레임의 각 행에서 중복을 제거하십시오.

각 행에 대해 중복 값이 ​​열 주위에 분산되어 있습니다. 데이터 프레임을 transposing하고 컬럼을 제거하려고했습니다. 그러나 그것은 추락합니다. 나는 전치 후

내가 사용 :

for(i in 1:ncol(df)){ 

     #replicate column i without duplicates, fill blanks with NAs 
     df <- cbind.fill(df,unique(df[,1]), fill = NA) 
     #rename the new column 
     colnames(df)[n+1] <- colnames(df)[1] 
     #delete the old column 
     df[,1] <- NULL 
} 

하지만 그 결과 지금까지.

누구나 아이디어가 있는지 알고 싶습니다.

최고

답변

0

각 열의 중복 된 값을 NA로 바꾸시겠습니까?

이것은 여러 가지 방법으로 수행 할 수 있습니다.

먼저 일부 데이터 : purrr 라이브러리

set.seed(7) 
df <- data.frame(x = sample(1: 20, 50, replace = T), 
       y = sample(1: 20, 50, replace = T), 
       z = sample(1: 20, 50, replace = T)) 
head(df, 10) 
#output 
    x y z 
1 20 12 8 
2 8 15 10 
3 3 16 10 
4 2 13 8 
5 5 15 13 
6 16 8 7 
7 7 4 20 
8 20 4 1 
9 4 8 16 
10 10 6 5 

:

library(purrr) 
map_dfc(df, function(x) ifelse(duplicated(x), NA, x)) 
#output 
# A tibble: 50 x 3 
     x  y  z 
    <int> <int> <int> 
1 20 12  8 
2  8 15 10 
3  3 16 NA 
4  2 13 NA 
5  5 NA 13 
6 16  8  7 
7  7  4 20 
8 NA NA  1 
9  4 NA 16 
10 10  6  5 
# ... with 40 more rows 

베이스 R 적용과

as.data.frame(apply(df, 2, function(x) ifelse(duplicated(x), NA, x)))