如何在R中将矢量分成两个矢量

时间:2013-11-29 10:05:17

标签: r vector

在函数之后,我将矩阵作为一个向量

实施例

  [1] "M.                                "     "Mueller                                  "
  [3] "C.                                "     "Mueller                                  "
  [5] "L.                                "     "Mueller                                  "
  [7] "U.                                "     "Mueller                                  "
  [9] "D.                                "     "Mueller                                  ",

现在我想把它分成2个载体,一个载有所有“Mueller”,一个带有M.,C。等

提前致谢! 编辑: 分裂

我得到了一些奇怪的结果
test1<-split(test, c(1,2))
vorname<-test1[[1]]

structure(c("M.                                ", "C.                               ", 
"L.                             ", "U.                                ", 

    ..), .Names = c("1.vorname", "2.vorname", "3.vorname", "4.vorname", 
    "5.vorname", "6.vorname", "7.vorname", "8.vorname", "9.vorname", 

vorname是第1列的名称

编辑2:

structure(list(vorname = structure(c(48L, 48L, 38L, 65L, 73L, 
67L, 17L, 75L, 39L, 61L), .Label = c("Adrian                               ", 
"Andrea                               ", "Andreas                              ", 
"Angela                               ", "Anna Lena                            ", 
"AnnaElisabeth                        ", "AnnaSarah                            ", 
"Beate                                ", "Bernd                                ", 
"Birgit                               ", "BrunoHeribert                        ", 
"Carina                               ", "Carolin                              ", 
"Chris Joachim                        ", "Christa                              ", 
"Christian                            ", "Christopher                          ", 
"Cornelia                             ", "Daniel Markus                        ", 
"DavidBenjamin                        ", "Dennis                               ", 
"Elena                                ", "Elisabeth                            ", 
"Erhard                               ", "Frieder                              ", 
"Friederike                           ", "HansJörg                             ", 
"Harald                               ", "Heike                                ", 
"Heinz                                ", "Ilka                                 ", 
"Jan                                  ", "Janina                               ", 
"Janine                               ", "Jessica Louise                       ", 
"JörgHenning                          ", "Jürgen                               ", 
"Karin                                ", "Karl Heinz                           ", 
"Katharina                            ", "Kathrin                              ", 
"Katja                                ", "KlausDieter                          ", 
"Ljubow                               ", "Luise                                ", 
"Lutz                                 ", "Mandy                                ", 
"Marco                                ", "Marius                               ", 
"Mattea                               ", "Matthias                             ", 
"Maurice                              ", "Maximilian                           ", 
"Melanie                              ", "Melissa                              ", 
"Michael                              ", "Miriam                               ", 
"Norbert                              ", "Oliver                               ", 
"Peer                                 ", "Peggy                                ", 
"RalfMichael                          ", "Rebekka                              ", 
"Regina                               ", "Rolf                                 ", 
"Rosalinde                            ", "Sandra                               ", 
"Silvia                               ", "Stefan                               ", 
"Stefanie                             ", "Stephan                              ", 
"Susan                                ", "Svitlana                             ", 
"SwaantjeCathrin                      ", "Thomas                               ", 
"Tino                                 ", "UdoHeinz                             ", 
"Ulrike                               ", "Uwe                                  ", 
"Viktoria                             "), class = "factor"), 
    nachname = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "Müller                                  ", class = "factor")), .Names = c("vorname", 
"nachname"), row.names = c(NA, 10L), class = "data.frame")

2 个答案:

答案 0 :(得分:2)

您的问题包括一个方便的功能:split

> split(myvec, c(1,2))
$`1`
[1] "M.                                " "C.                                "
[3] "L.                                " "U.                                "
[5] "D.                                "

$`2`
[1] "Mueller                                  " "Mueller                                  "
[3] "Mueller                                  " "Mueller                                  "
[5] "Mueller                                  "

以上将创建两个向量的列表。或者,您可以使用“回收”和逻辑向量将其分配给新的向量:

> (vec1 <- myvec[c(TRUE, FALSE)])
[1] "M.                                " "C.                                "
[3] "L.                                " "U.                                "
[5] "D.                                "
> (vec2 <- myvec[c(FALSE, TRUE)])
[1] "Mueller                                  " "Mueller                                  "
[3] "Mueller                                  " "Mueller                                  "
[5] "Mueller                                  "

注意: 此答案假定您的数据是成对的。

答案 1 :(得分:0)

你可以把它变回矩阵:

v <- as.character(1:10)
m <- matrix(v, ncol=2, byrow=TRUE)
v1 <- m[,1]
v2 <- m[,2]

> v
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

> m
     [,1] [,2]
[1,] "1"  "2" 
[2,] "3"  "4" 
[3,] "5"  "6" 
[4,] "7"  "8" 
[5,] "9"  "10"

> v1
[1] "1" "3" "5" "7" "9"

> v2
[1] "2"  "4"  "6"  "8"  "10"