1
votes

I need to get all the different variables (picking and choosing from one data set) into a new data set.

I have used subset and now it is saying it has 0 observations for 102 variables. Where have the observations gone? (I have tried all other things, "c", cbind, merge, data.frame. They all say that values must be logical or for data.frame, lengths don't match).

So if I do this:

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)))

I get:

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
2
Please show a small reproducible example and the code you tried. - akrun
hhmemshp2$commun)&(hhmemshp2$hhnum)&.... doesn't look right. Are these all logical columns? - akrun
I thought they were but I don't know anymore... how do I check whether they are logical columns? - Lee12345
you may want something like dplyr::select(hhmemshp2, commun, hhnum, ...) - scoa
is.logical, but I am not sure what you are doing with all these &. If you want to select the columns subset(hhmemshp2, select=c(...)) - akrun

2 Answers

1
votes

I may not be understanding you, but if you want to just extract certain columns, use, e.g.,

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

So in your case it would be:

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

If we need to subset the columns, use the select argument in subset

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

Or

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