2016-08-20 9 views
1

stringr 패키지를 사용하여 문자열을 열로 분리하려고합니다.문자 벡터 목록의 데이터 프레임을 균일 한 데이터 프레임으로 혼합했습니다.

> df <- dput(head(facs,3)) 
structure(list(geo_accession = structure(1:3, .Names = c("V2", 
"V3", "V4"), .Label = c("GSM1494875", "GSM1494877", "GSM1494879", 
"GSM1494881", "GSM1494883", "GSM1494885", "GSM1494887", "GSM1494889", 
"GSM1494891", "GSM1494893", "GSM1494895", "GSM1494897", "GSM1494899", 
"GSM1494901", "GSM1494903", "GSM1494906", "GSM1494908", "GSM1494910", 
"GSM1494912", "GSM1494914", "GSM1494917", "GSM1494919", "GSM1494921", 
"GSM1494923"), class = "factor"), title = structure(1:3, .Names = c("V2", 
"V3", "V4"), .Label = c("2818 - Akt treated TIL - repeat 1 - mAdbID:125971", 
"2818 - Akt treated TIL - repeat 2 - mAdbID:125972", "2818 - Akt treated TIL - repeat 3 - mAdbID:125973", 
"2818 - Akt treated TIL - repeat 4 - mAdbID:125974", "2818 - Untreated TIL - repeat 1 - mAdbID:125975", 
"2818 - Untreated TIL - repeat 2 - mAdbID:125976", "2818 - Untreated TIL - repeat 3 - mAdbID:125977", 
"2818 - Untreated TIL - repeat 4 - mAdbID:125978", "3289 - Akt treated TIL - repeat 1 - mAdbID:125979", 
"3289 - Akt treated TIL - repeat 2 - mAdbID:125980", "3289 - Akt treated TIL - repeat 3 - mAdbID:125981", 
"3289 - Akt treated TIL - repeat 4 - mAdbID:125982", "3289 - Untreated TIL - repeat 1 - mAdbID:125983", 
"3289 - Untreated TIL - repeat 2 - mAdbID:125984", "3289 - Untreated TIL - repeat 3 - mAdbID:125985", 
"3289 - Untreated TIL - repeat 4 - mAdbID:125986", "3784 - Akt treated TIL - repeat 1 - mAdbID:125987", 
"3784 - Akt treated TIL - repeat 2 - mAdbID:125988", "3784 - Akt treated TIL - repeat 3 - mAdbID:125989", 
"3784 - Akt treated TIL - repeat 4 - mAdbID:125990", "3784 - Untreated TIL - repeat 1 - mAdbID:125991", 
"3784 - Untreated TIL - repeat 2 - mAdbID:125992", "3784 - Untreated TIL - repeat 3 - mAdbID:125993", 
"3784 - Untreated TIL - repeat 4 - mAdbID:125994"), class = "factor")), .Names = c("geo_accession", 
"title"), row.names = c("GSM1494875", "GSM1494877", "GSM1494879" 
), class = "data.frame") 

지금은 2 열 데이터 프레임입니다.

> sapply(df, class) 
geo_accession   title 
"factor"  "factor" 

제가 str_split_fixed를 사용하는 경우

,

> df$title = str_split_fixed(df[,"title"], " - ", 4) 
> df 
      geo_accession title.1   title.2 title.3  title.4 
GSM1494875 GSM1494875 2818 Akt treated TIL repeat 1 mAdbID:125971 
GSM1494877 GSM1494877 2818 Akt treated TIL repeat 2 mAdbID:125972 

> sapply(df, class) 
geo_accession   title 
"factor"  "matrix" 

어떤 I보고자하는 것은 5 열 dataframe 대신 2 열 혼합 dataframe (계수 행렬)이다.

> df 
     geo_accession title1   title2 title3  title4 
GSM1494875 GSM1494875 2818 Akt treated TIL repeat 1 mAdbID:125971 
GSM1494877 GSM1494877 2818 Akt treated TIL repeat 2 mAdbID:125972 

> sapply(df, class) 
geo_accession  title.1  title.2  title.3  title.4 
"factor"  "factor"  "factor"  "factor"  "factor" 

혼합 된 데이터 프레임을 정리하는 방법을 모르겠습니다. 이것을 5 열 데이터 프레임으로 어떻게 변환 할 수 있습니까?

+0

참조'tidyr :: separate' –

+0

재현에 데이터를 공유하기 위해'dput'을 사용하십시오 재현 예를 –

+0

을 제공하십시오 방법에 따라 R 태그 설명 –

답변

1

당신은 tidyr 패키지에서 separate를 사용할 수 있습니다?

library(tidyr) 
df %>% separate(title, paste0('title',1:4) ,' - ') 

      geo_accession title1   title2 title3  title4 
1 GSM1494875 GSM1494875 2818 Akt treated TIL repeat 1 mAdbID:125971 
2 GSM1494877 GSM1494877 2818 Akt treated TIL repeat 2 mAdbID:125972 
+0

그 덕분에 잘됐다! 함께 문자열 작업을 할 수있게되면 훨씬 더 읽을 수있는 코드가됩니다 :) – seraphim711

1

str_split_fixed의 출력은 listvectors입니다. 하나의 열에 list을 할당하면 문제의 원인이됩니다. 한 가지 방법은 여러 컬럼에 할당하거나, 우리가

facs1 <- do.call(data.frame, facs) 

즉, do.call(data.frame으로 문제를 해결할 수 있습니다 또는 우리가 이전과 - 다음 cbind을 한 후 공백을 제거한 후 read.csv/read.tablebase R 방법을 사용할 수있다 첫 번째 열

facs2 <- cbind(facs[1], read.csv(text=gsub("\\s+-\\s+", "-", facs$title), 
      sep="-", header=FALSE, col.names = paste0("title.", 1:4), stringsAsFactors=FALSE)) 
facs2 
#   geo_accession title.1   title.2 title.3  title.4 
#GSM1494875 GSM1494875 2818 Akt treated TIL repeat 1 mAdbID:125971 
#GSM1494877 GSM1494877 2818 Akt treated TIL repeat 2 mAdbID:125972 

str(facs2) 
#'data.frame': 2 obs. of 5 variables: 
#$ geo_accession: chr "GSM1494875" "GSM1494877" 
#$ title.1  : int 2818 2818 
#$ title.2  : chr "Akt treated TIL" "Akt treated TIL" 
#$ title.3  : chr "repeat 1" "repeat 2" 
#$ title.4  : chr "mAdbID:125971" "mAdbID:125972" 

또는 소형 옵션에서 cSplit입니다 splitstackshape

library(splitstackshape) 
cSplit(facs, "title", " - ") 
+1

위대한 설명과 추가 방법을 보내 주셔서 감사합니다! – seraphim711