Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

How do I make a Foorloop change each row instead of the whole column in R?

everyone,

I am very new to R, and I have some questions with my code.

I have a dataframe that has 1,500 observations. I created a column for low income, and I need to assign values based on the value of another variable.

What I want is that if the value on df$income is 4:13, the loop assigns value 1. Otherwise, if it's 14 :17, assign a value 0.

I have the loop like this

low_inc <- NA

vote$low_inc <- low_inc

for(i in vote$income){

if (vote$income == 4||5||6||7||8||9||10||11||12||13) {

vote$low_inc <- 1

} else if (vote$income == 14||15||16||17){

vote$low_inc <- 0

}

}

I think the issue is that I am changing the whole column instead of each individually. Could anyone help me? Thank you before hand.

question from:https://stackoverflow.com/questions/65893089/how-do-i-make-a-foorloop-change-each-row-instead-of-the-whole-column-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...