问题描述:

I am quite new to R, and I need R to recognize some of data as binary. So I made a survey and I'd like R to analyze the Gender, Like, and Share columns as binary - so I can ggplot them properly.

head(d)

Gender Age Like Share

1 Male 26 No 2

2 Male 23 Yes 0

3 Female 21 No 4

4 Male 19 No 0

5 Female 20 Yes 0

6 Male 23 No 4

The thing is that I would like to evaluate if there's a corrolation between certain factors - and to do so, I want to do a regression line that would look like this. But the problem is that I always get this error message

M <- ggplot(data=d,aes(x=Age,colour=Share) + geom_point())

Error in aes(x = Age, colour = Share) + geom_point() :

non-numeric argument to binary operator

So if you could help me out with those two problems, that'd be awesome.

One last thing, I don't know if this could help, but in case, I'll give it to you ...

 sapply(D,class)

Gender Age Like Share

"integer" "numeric" "integer" "integer"

sapply(D,mode)

Gender Age Like Share

"numeric" "numeric" "numeric" "numeric"

Thanks a lot in advance !

网友答案:

The easiest way to make those categories binary would be something like this:

d$GenderBinary <- 0
d$GenderBinary[d$Gender == "Male"] <- 1
d$LikeBinary <- 0
d$LikeBinary[d$Gender == "Yes"] <- 1

Not sure how you expect to make a column with more than two categories binary, though ("Share").

For the ggplot error, it looks like you have forgotten to supply the y variable(s) (which presumably would be your binary variables).

相关阅读:
Top