[R] Create a new variable

Thomas Jensen thomas.jensen at eup.gess.ethz.ch
Tue Mar 30 18:31:05 CEST 2010


Dear R-list,

Sorry for spamming the list lately, I am just learning the more advanced
aspects of R! 

I have some data that looks like this:

Out Country1 Country 2 Country 3 ... CountryN
1	1	1	1		1
0	1	1	0		1
1	1	0	1		0

I want to create a new variable that counts the number of zeros in every
row whenever Out is equal to 1, and else it is a zero, so it would look
like this:

new_var
0 
0
2

I have tried the following:

for (i in length(Out)){
if (Out == 1) {new_var <- sum(dat[i,] != 1)}
else {new_var <- 0}
}

but this gives me an error message.

Best, Thomas



More information about the R-help mailing list