2017-02-22 6 views
3

나는 tibble 양식에 DF라는이 : 내가 가지고 그것을 재 배열하고 싶은중복 된 ID와 데이터 프레임을 순서 변경

df <- tbl_df(
data.frame(sample = rep(c("SRM1", "SAM1"), each = 6), 
nuclide = rep(c("Pb206", "Pb207", "Pb208"), 4), 
intensity = c(200, 250, 301, 202, 254, 305, 154, 262, 311, 157, 261, 325))) 

:

sample nuclide intensity 
SRM1 Pb206 200 
SRM1 Pb207 250 
SRM1 Pb208 301 
SRM1 Pb206 202 
SRM1 Pb207 254 
SRM1 Pb208 305 
SAM1 Pb206 154 
SAM1 Pb207 262 
SAM1 Pb208 311 
SAM1 Pb206 157 
SAM1 Pb207 261 
SAM1 Pb208 325 

그것은에 의해 생성 될 수있다

sample Pb208 Pb207 Pb206 
SRM1 301 250 200 
SRM1 305 254 202 
SAM1 311 262 157 
SAM1 325 261 204 

은 내가 tidyr PA와 시도 ckage를 사용하여 :

df %>% 
    select(sample, nuclide, intensity) %>% 
    group_by(sample) %>% 
    mutate(row = 1:n()) %>% 
    spread(nuclide, intensity) %>% select(-row) 

그러나 원하지 않는 NAs가 많은 경우 다른 결과가 나타납니다.

변환하는 동안 데이터 집합의 샘플 이름은 원래 순서대로 유지되어야하며 집계 함수는 사용되지 않는 것이 매우 중요합니다. 특히 첫 번째 조건은 이전에 게시 된 다른 유사한 문제와 다른 점을 만듭니다.

그런 다음 솔루션은 20000 개가 넘는 행이있는 훨씬 더 큰 데이터 세트에 적용됩니다.

+0

'library (data.table); (샘플, 핵종)], id + sample ~ 핵종, value.var = '강도')' – Jaap

+0

불행히도,이 해결책은 _df_에 정의 된 샘플의 원래 순서. – Ndr

+0

나중에 다시 주문하십시오. – Jaap

답변

2

사용 :

lvls <- as.character(unique(df$sample)) 

library(tidyverse) # this will load 'dplyr' and 'tidyr' among others 
df %>% 
    group_by(sample, nuclide) %>% 
    mutate(id = row_number()) %>% 
    spread(nuclide, intensity) %>% 
    ungroup() %>%        # needed to be able to modify the 'sample' variable 
    mutate(sample = factor(sample, levels = lvls)) %>% 
    arrange(sample) %>% 
    select(sample, Pb208:Pb206) 

는 제공 : 또는

sample Pb208 Pb207 Pb206 
    (fctr) (dbl) (dbl) (dbl) 
1 SRM1 301 250 200 
2 SRM1 305 254 202 
3 SAM1 311 262 154 
4 SAM1 325 261 157 

또는 (당신은 단지 내림차순을 원하는 경우) :

df %>% 
    group_by(sample, nuclide) %>% 
    mutate(id = row_number()) %>% 
    spread(nuclide, intensity) %>% 
    arrange(desc(sample)) %>% 
    select(sample, Pb208:Pb206) 

data.table을 사용하는 대안 :

library(data.table) 
dcast(setDT(df), sample + rowid(sample, nuclide) ~ nuclide, 
     value.var = 'intensity')[, sample := factor(sample, levels = lvls) 
           ][order(sample)] 
+0

이 코드는 작동하지만, 더 많은 샘플 ID가있는 비교적 긴 데이터 세트에 같은 코드를 적용해야하므로 손으로 레벨의 순서를 지정하고 싶지 않습니다. – Ndr

+0

@Ndr이 답변을 업데이트했습니다. – Jaap