簡體   English   中英

R將數據幀的不同列添加到單個列

[英]R add different column of a data frame to a single column

我有一個2050行乘202列的大型數據框。 我使用命令read.spss()從SPSS讀取數據。 這些是因子變量。

data<-read.spss("filename.sav",to.data.frame=TRUE,reencode='utf-8')
dim(data)
[1] "data.frame"
dim(data)
[1] 2050  202
class(data[1,57])
[1] "factor"
class(data$aq21a) # data$aq21a is 57th column
[1] "NULL"

現在我想將列57到61 (data$aq21a,data$aq21b,data$aq21c,data$aq21d,data$aq21e)到新變量aq21 ,如下所示

aq21<-rbind(data$aq21a,data$aq21b,data$aq21c,data$aq21d,data$aq21e)

但這並不能提供所需的結果。 我想要10250 x 1的向量

class(aq21)
[1] "matrix"
dim(aq21)
[1]    5 2050

樣本數據是

head(data[,57:60])
               bq21a               bq21b               bq21c               bq21d
1 Rich / Independent           Efficient                <NA>                <NA>
2   Known / Familiar           Efficient                <NA>                <NA>
3  Relative / Friend Educated / Academic         Accountable                <NA>
4       Truthfulness           Behaviour          Good/Great Educated / Academic
5          Behaviour   Relative / Friend Educated / Academic    Known / Familiar
6          Behaviour   Relative / Friend                <NA>                <NA>

我想要這種類型的結果

               bq21a
1  Rich / Independent
2    Known / Familiar
3   Relative / Friend
4        Truthfulness
5           Behaviour
6           Behaviour
7           Efficient
8           Efficient
9 Educated / Academic
10          Behaviour
11  Relative / Friend
... and so on

我得到的結果樣本,不是必需的

aq21[1:5,1:10]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   11   24   19   35   22   22    3   22   NA     2
[2,]    6    6   18   22   19   19   31   31   NA     5
[3,]   NA   NA    9    2   18   NA   26   NA   NA    31
[4,]   NA   NA   NA   18   24   NA   NA   NA   NA    NA
[5,]   NA   NA   NA   23   NA   NA   NA   NA   NA    NA

它有什么問題,如何獲得正確答案?

假設所有相關列都是factors ,例如:

   data <- structure(list(bq21a = structure(c(4L, 2L, 3L, 5L, 1L, 1L), .Label = c("Behaviour", 
   "Known / Familiar", "Relative / Friend", "Rich / Independent", 
   "Truthfulness"), class = "factor"), bq21b = structure(c(3L, 3L, 
   2L, 1L, 4L, 4L), .Label = c("Behaviour", "Educated / Academic", 
  "Efficient", "Relative / Friend"), class = "factor"), bq21c = structure(c(NA, 
   NA, 1L, 3L, 2L, NA), .Label = c("Accountable", "Educated / Academic", 
  "Good/Great"), class = "factor"), bq21d = structure(c(NA, NA, 
   NA, 1L, 2L, NA), .Label = c("Educated / Academic", "Known / Familiar"
   ), class = "factor")), .Names = c("bq21a", "bq21b", "bq21c", 
   "bq21d"), class = "data.frame", row.names = c("1", "2", "3", 
   "4", "5", "6"))

   as.numeric(data[,1])
   #[1] 4 2 3 5 1 1
   as.numeric(data[,2])
   #[1] 3 3 2 1 4 4
   as.numeric(data[,3])
   #[1] NA NA  1  3  2 NA

當您執行rbind ,您將得到從factornumeric的轉換形式的結果,如上所示。

  rbind(data$bq21a, data$bq21b, data$bq21c, data$bq21d) 
  #    [,1] [,2] [,3] [,4] [,5] [,6]
  #[1,]    4    2    3    5    1    1
  #[2,]    3    3    2    1    4    4
  #[3,]   NA   NA    1    3    2   NA
  #[4,]   NA   NA   NA    1    2   NA

在使用read.csvread.table讀取數據時,可以使用stringsAsFactors=FALSE 如果是這樣的話:

 dat1 <- data.frame(bq21a=c(rbind(data$bq21a, data$bq21b, data$bq21c, data$bq21d)))
 head(dat1)
 #           bq21a
 #1 Rich / Independent
 #2          Efficient
 #3               <NA>
 #4               <NA>
 #5   Known / Familiar
 #6          Efficient

更新

或嘗試:

   dat2 <- data.frame(bq21a=c(t(data))) #wouldn't matter if the columns are `factors`
   #in your dataset the code would be
   #dat2 <- data.frame(bq21a= c(t(data[,57:61[)))  

   identical(dat1, dat2)
   #[1] TRUE

根據Ananda和akrun的建議,我使用了以下代碼

aq21<-data.frame(Col=unlist(data[,57:61]))

這就解決了問題。 但是我仍然不明白為什么rbind不起作用?

我們必須使用unlist使rbind正常工作嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM