簡體   English   中英

將許多不同的變量整合到一個數據框中

[英]lots of different variables into one data frame

我需要將所有不同的變量(從一個數據集中選擇和選擇)放入一個新的數據集中。

我用過子集,現在說它對102個變量有0個觀測值。 觀察結果哪里去了? (我嘗試了所有其他操作,例如“ c”,cbind,merge,data.frame。他們都說值必須是邏輯的,或者對於data.frame,長度不匹配)。

因此,如果我這樣做:

prevsex1<-subset(hhmemshp2,((hhmemshp2$commun)&(hhmemshp2$hhnum)& 
            (hhmemshp2$surveyyr)& (hhmemshp2$surveypl)&
            year& (hhmemshp2$yrborn)& (hhmemshp2$usyr1)& 
            (hhmemshp2$hhmemshp)& (hhmemshp2$sex)& 
            (hhmemshp2$allmig1)& (hhmemshp2$manmig1)& 
            (hhmemshp2$femmig1)& (hhmemshp2$allage)& 
            (hhmemshp2$manage)& (hhmemshp2$femage1)& 
            (hhmemshp2$alllive)& (hhmemshp2$manlive)& 
            (hhmemshp2$femlive)& (hhmemshp2$weight)))

我得到:

Error in (hhmemshp2$commun) & (hhmemshp2$hhnum) & (hhmemshp2$surveyyr) &  : 
  operations are possible only for numeric, logical or complex types
In addition: Warning message:
In (hhmemshp2$commun) & (hhmemshp2$hhnum) & (hhmemshp2$surveyyr) &  :
  longer object length is not a multiple of shorter object length

我可能不太了解您,但是如果您只想提取某些列,請使用例如

df <- mtcars[,c("mpg","wt")]
head(df,2)
#               mpg    wt
# Mazda RX4      21 2.620
# Mazda RX4 Wag  21 2.875

因此,在您的情況下,它將是:

prevsex1<-hhmemshp2[,c("commun","hhnum","surveyyr",...)]

如果需要對列進行子集化,請在subset使用select參數

data(mtcars)
df1 <- subset(mtcars, select=c('mpg', 'wt'))

要么

df1 <- subset(mtcars, select=names(mtcars)[c(1,6)])

暫無
暫無

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

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