Rilcon42
Rilcon42

Reputation: 9765

removing columns via subset throws unary invalid argument error

I have a dataframe with rather odd column names (it is a combination of several other dataframes). Every time I try to subset and remove a column it gives me an error Error in -c("surveys$gender") : invalid argument to unary operator. Can someone explain why this is happening?

df1<-NULL
df1$a<-c(1,2,3,4,5)
df1$b<-c(4,5,2,6,1)
df1$c<-c(0,9,0,6,3)
surveys<-NULL
surveys$gender<-c(0,0,0,1,1)
df1<-as.data.frame(df1)

df1<-cbind(df1,surveys$gender)
male<-subset(df1,'surveys$gender'<1,select=-c('surveys$gender')) #trying to get only 0 and remove the gender column from the result

Upvotes: 2

Views: 4760

Answers (2)

Whitebeard
Whitebeard

Reputation: 6193

Is this helpful?

male <- df1[df1[['surveys$gender']] < 1, ]
male[['surveys$gender']] <- NULL
male

  a b c
1 1 4 0
2 2 5 9
3 3 2 0

Upvotes: 3

David Robinson
David Robinson

Reputation: 78590

Change your subset line to:

df1 <- NULL
df1$a<-c(1,2,3,4,5)
df1$b<-c(4,5,2,6,1)
df1$c<-c(0,9,0,6,3)
df1$gender<-c(0,0,0,1,1)
male <- subset(df1, gender < 1, select = -gender)

That is, use bare column names rather than quotes.

Upvotes: 4

Related Questions