将行条目传输到列,将列条目传输到行

时间:2014-11-19 00:18:25

标签: r

我有一个数据框,我通过执行以下操作成功地将字符串拆开了#34; test500 = strsplit(SelectedRNumberOnly[ ,"Quality_Flag"], "")。现在我得到了3个观察(行)和184个变量(列)的答案。但它实际上是另一种方式。如何将3行184列的矩阵传输到184行3列的数据帧? 提前谢谢了! 约翰内斯

以下是包含打印到控制台的代码:

test500 = SelectedRNumberOnly[ ,"Quality_Flag"]
          print(test500)
  [1] "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..."
 [23] "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..."
 [45] "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "A.." "…"   "…"   "A.."
 [67] "…"   "…"   "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..."
 [89] "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..."
[111] "..." "A.." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "A.." "..." "..." "..." "..."
[133] "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..."
[155] "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..." "..."
[177] "..." "..." "..." "..." "..." "..." "..." "..."

test501 = strsplit(SelectedRNumberOnly[ ,"Quality_Flag"], "")
          print(test501)` `[[1]]
[1] "." "." "."

[[2]]
[1] "." "." "."

[[3]]
[1] "." "." "."

[[4]]
[1] "." "." "."

[[5]]
[1] "." "." "."

[[6]]
[1] "." "." "."

[[7]]
[1] "." "." "."

[[8]]
[1] "." "." "."

[[9]]
[1] "." "." "."

[[10]]
[1] "." "." "."

[[11]]
[1] "." "." "."

[[12]]
[1] "." "." "."

[[13]]
[1] "." "." "."

[[14]]
[1] "." "." "."

[[15]]
[1] "." "." "."

[[16]]
[1] "." "." "."

[[17]]
[1] "." "." "."

[[18]]
[1] "." "." "."

[[19]]
[1] "." "." "."

[[20]]
[1] "." "." "."

[[21]]
[1] "." "." "."

[[22]]
[1] "." "." "."

[[23]]
[1] "." "." "."

[[24]]
[1] "." "." "."

[[25]]
[1] "." "." "."

[[26]]
[1] "." "." "."

[[27]]
[1] "." "." "."

[[28]]
[1] "." "." "."

[[29]]
[1] "." "." "."

[[30]]
[1] "." "." "."

[[31]]
[1] "." "." "."

[[32]]
[1] "." "." "."

[[33]]
[1] "." "." "."

[[34]]
[1] "." "." "."

[[35]]
[1] "." "." "."

[[36]]
[1] "." "." "."

[[37]]
[1] "." "." "."

[[38]]
[1] "." "." "."

[[39]]
[1] "." "." "."

[[40]]
[1] "." "." "."

[[41]]
[1] "." "." "."

[[42]]
[1] "." "." "."

[[43]]
[1] "." "." "."

[[44]]
[1] "." "." "."

[[45]]
[1] "." "." "."

[[46]]
[1] "." "." "."

[[47]]
[1] "." "." "."

[[48]]
[1] "." "." "."

[[49]]
[1] "." "." "."

[[50]]
[1] "." "." "."

[[51]]
[1] "." "." "."

[[52]]
[1] "." "." "."

[[53]]
[1] "." "." "."

[[54]]
[1] "." "." "."

[[55]]
[1] "." "." "."

[[56]]
[1] "." "." "."

[[57]]
[1] "." "." "."

[[58]]
[1] "." "." "."

[[59]]
[1] "." "." "."

[[60]]
[1] "." "." "."

[[61]]
[1] "." "." "."

[[62]]
[1] "." "." "."

[[63]]
[1] "A" "." "."

[[64]]
[1] "…"

[[65]]
[1] "…"

[[66]]
[1] "A" "." "."

[[67]]
[1] "…"

[[68]]
[1] "…"

[[69]]
[1] "." "." "."

[[70]]
[1] "." "." "."

[[71]]
[1] "." "." "."

[[72]]
[1] "." "." "."

[[73]]
[1] "." "." "."

[[74]]
[1] "." "." "."

[[75]]
[1] "." "." "."

[[76]]
[1] "." "." "."

[[77]]
[1] "." "." "."

[[78]]
[1] "." "." "."

[[79]]
[1] "." "." "."

[[80]]
[1] "." "." "."

[[81]]
[1] "." "." "."

[[82]]
[1] "." "." "."

[[83]]
[1] "." "." "."

[[84]]
[1] "." "." "."

[[85]]
[1] "." "." "."

[[86]]
[1] "." "." "."

[[87]]
[1] "." "." "."

[[88]]
[1] "." "." "."

[[89]]
[1] "." "." "."

[[90]]
[1] "." "." "."

[[91]]
[1] "." "." "."

[[92]]
[1] "." "." "."

[[93]]
[1] "." "." "."

[[94]]
[1] "." "." "."

[[95]]
[1] "." "." "."

[[96]]
[1] "." "." "."

[[97]]
[1] "." "." "."

[[98]]
[1] "." "." "."

[[99]]
[1] "." "." "."

[[100]]
[1] "." "." "."

[[101]]
[1] "." "." "."

[[102]]
[1] "." "." "."

[[103]]
[1] "." "." "."

[[104]]
[1] "." "." "."

[[105]]
[1] "." "." "."

[[106]]
[1] "." "." "."

[[107]]
[1] "." "." "."

[[108]]
[1] "." "." "."

[[109]]
[1] "." "." "."

[[110]]
[1] "." "." "."

[[111]]
[1] "." "." "."

[[112]]
[1] "A" "." "."

[[113]]
[1] "." "." "."

[[114]]
[1] "." "." "."

[[115]]
[1] "." "." "."

[[116]]
[1] "." "." "."

[[117]]
[1] "." "." "."

[[118]]
[1] "." "." "."

[[119]]
[1] "." "." "."

[[120]]
[1] "." "." "."

[[121]]
[1] "." "." "."

[[122]]
[1] "." "." "."

[[123]]
[1] "." "." "."

[[124]]
[1] "." "." "."

[[125]]
[1] "." "." "."

[[126]]
[1] "." "." "."

[[127]]
[1] "." "." "."

[[128]]
[1] "A" "." "."

[[129]]
[1] "." "." "."

[[130]]
[1] "." "." "."

[[131]]
[1] "." "." "."

[[132]]
[1] "." "." "."

[[133]]
[1] "." "." "."

[[134]]
[1] "." "." "."

[[135]]
[1] "." "." "."

[[136]]
[1] "." "." "."

[[137]]
[1] "." "." "."

[[138]]
[1] "." "." "."

[[139]]
[1] "." "." "."

[[140]]
[1] "." "." "."

[[141]]
[1] "." "." "."

[[142]]
[1] "." "." "."

[[143]]
[1] "." "." "."

[[144]]
[1] "." "." "."

[[145]]
[1] "." "." "."

[[146]]
[1] "." "." "."

[[147]]
[1] "." "." "."

[[148]]
[1] "." "." "."

[[149]]
[1] "." "." "."

[[150]]
[1] "." "." "."

[[151]]
[1] "." "." "."

[[152]]
[1] "." "." "."

[[153]]
[1] "." "." "."

[[154]]
[1] "." "." "."

[[155]]
[1] "." "." "."

[[156]]
[1] "." "." "."

[[157]]
[1] "." "." "."

[[158]]
[1] "." "." "."

[[159]]
[1] "." "." "."

[[160]]
[1] "." "." "."

[[161]]
[1] "." "." "."

[[162]]
[1] "." "." "."

[[163]]
[1] "." "." "."

[[164]]
[1] "." "." "."

[[165]]
[1] "." "." "."

[[166]]
[1] "." "." "."

[[167]]
[1] "." "." "."

[[168]]
[1] "." "." "."

[[169]]
[1] "." "." "."

[[170]]
[1] "." "." "."

[[171]]
[1] "." "." "."

[[172]]
[1] "." "." "."

[[173]]
[1] "." "." "."

[[174]]
[1] "." "." "."

[[175]]
[1] "." "." "."

[[176]]
[1] "." "." "."

[[177]]
[1] "." "." "."

[[178]]
[1] "." "." "."

[[179]]
[1] "." "." "."

[[180]]
[1] "." "." "."

[[181]]
[1] "." "." "."

[[182]]
[1] "." "." "."

[[183]]
[1] "." "." "."

[[184]]
[1] "." "." "." ``  test502 = t(test501)
          print(test502)` `     [,1]        [,2]        [,3]        [,4]        [,5]        [,6]        [,7]        [,8]        [,9]        [,10]       [,11]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,12]       [,13]       [,14]       [,15]       [,16]       [,17]       [,18]       [,19]       [,20]       [,21]       [,22]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,23]       [,24]       [,25]       [,26]       [,27]       [,28]       [,29]       [,30]       [,31]       [,32]       [,33]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,34]       [,35]       [,36]       [,37]       [,38]       [,39]       [,40]       [,41]       [,42]       [,43]       [,44]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,45]       [,46]       [,47]       [,48]       [,49]       [,50]       [,51]       [,52]       [,53]       [,54]       [,55]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,56]       [,57]       [,58]       [,59]       [,60]       [,61]       [,62]       [,63]       [,64] [,65] [,66]       [,67] [,68]
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 "…"   "…"   Character,3 "…"   "…"  
     [,69]       [,70]       [,71]       [,72]       [,73]       [,74]       [,75]       [,76]       [,77]       [,78]       [,79]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,80]       [,81]       [,82]       [,83]       [,84]       [,85]       [,86]       [,87]       [,88]       [,89]       [,90]      
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,91]       [,92]       [,93]       [,94]       [,95]       [,96]       [,97]       [,98]       [,99]       [,100]      [,101]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,102]      [,103]      [,104]      [,105]      [,106]      [,107]      [,108]      [,109]      [,110]      [,111]      [,112]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,113]      [,114]      [,115]      [,116]      [,117]      [,118]      [,119]      [,120]      [,121]      [,122]      [,123]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,124]      [,125]      [,126]      [,127]      [,128]      [,129]      [,130]      [,131]      [,132]      [,133]      [,134]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,135]      [,136]      [,137]      [,138]      [,139]      [,140]      [,141]      [,142]      [,143]      [,144]      [,145]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,146]      [,147]      [,148]      [,149]      [,150]      [,151]      [,152]      [,153]      [,154]      [,155]      [,156]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,157]      [,158]      [,159]      [,160]      [,161]      [,162]      [,163]      [,164]      [,165]      [,166]      [,167]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,168]      [,169]      [,170]      [,171]      [,172]      [,173]      [,174]      [,175]      [,176]      [,177]      [,178]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3 Character,3
     [,179]      [,180]      [,181]      [,182]      [,183]      [,184]     
[1,] Character,3 Character,3 Character,3 Character,3 Character,3 Character,3

总结一下:我有一个数据框SelectedRNumberOnly,我确实要将列Quality_Flag分开,这样我最终会有列Quality_Flag_1Quality_Flag_2Quality_Flag_3在我的数据框SelectedRNumberOnly

1 个答案:

答案 0 :(得分:0)

试试这个:

# download and install plyr package
install.packages("plyr")
library(plyr)

# select Quality_Flag column
test500 <- SelectedRNumberOnly$Quality_Flag

# clean up the ugly observations
# this will make the code not reproducible
test500[c(64:65, 67:68)] <- "..."

# put this into ldplyr
test501 <- strsplit(test500, '')
test502 <- ldply(test501)

# rename the columns of test502
names(test502) <- c('Quality_Flag_1',
                    'Quality_Flag_2',
                    'Quality_Flag_3')

# append data set test502 to the original SelectedRNumberOnly
SelectedRNumberOnly <- data.frame(cbind(SelectedRNumberOnly, test502))