R将数据帧的不同列添加到单个列
我有一个2050行202列的大数据框。我使用命令R将数据帧的不同列添加到单个列,r,matrix,vector,dataframe,spss,R,Matrix,Vector,Dataframe,Spss,我有一个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 colum
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"
但这并没有给出所需的结果。我想要一个10250乘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>
我得到的结果样本,不符合要求
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
它有什么问题,我怎样才能得到正确的答案?根据阿南达和阿克伦的建议,我使用了以下代码
aq21<-data.frame(Col=unlist(data[,57:61]))
aq21假设相关列都是因子,例如:
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
使用read.csv
或read.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
dat1melt
来自“重塑2”<代码>未列出的< /代码>从基础R >代码> AQ21@ Prabu。如果您想考虑行顺序,最好使用<代码>数据。框架(BQ21A= C(T(data))< <代码> >代码> unList< /Cord>将获得您认为不是您想要的列顺序。
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(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
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