

> if (any(socscore<=3)) q<-0 else q=1
> q
[1] 0
Why am I only getting 1 answer?
Thanks in advance,
Ernie
One of the hardest things to remember when using R compared to other programming languages, is that almost everything you do is a vector operation. When you do q <- 0, R is going to apply the "<- 0" to the entire vector of q. And if you haven't created q before this, q will just be set equal to either 0 or 1. What you're asking R to do is if any value in socscore is less or equal to than 3, assign 0 to q, otherwise assign 1 to q. I'm assuming you want q to be a vector of 0's and 1's corresponding to whether socscore <= 3 or not?
There are a couple ways to do that. You can either use a loop:
q <- rep(NA, length(socscore))
for ( i in 1:length(socscore) {

15: Actor Rick Malambri

JULY 22: Rick Malambri (R)

Sharni Vinson, Rick Malambri,

Rick Malambri is his name,

Luke (rick malambri) and

Rick Malambri is one of the

Rick Malambri,

In this photo: Rick Malambri

is called Rick Malambri

Actors Rick Malambri,

(L-R) Actor Rick Malambri,

Actor Rick Malambri (L) and

Cast: Rick Malambri, Adam G.
> q
[1] 0
Why am I only getting 1 answer?
Thanks in advance,
Ernie
One of the hardest things to remember when using R compared to other programming languages, is that almost everything you do is a vector operation. When you do q <- 0, R is going to apply the "<- 0" to the entire vector of q. And if you haven't created q before this, q will just be set equal to either 0 or 1. What you're asking R to do is if any value in socscore is less or equal to than 3, assign 0 to q, otherwise assign 1 to q. I'm assuming you want q to be a vector of 0's and 1's corresponding to whether socscore <= 3 or not?
There are a couple ways to do that. You can either use a loop:
q <- rep(NA, length(socscore))
for ( i in 1:length(socscore) {

No comments:
Post a Comment