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
Hello, Thomas Jensen wrote:> 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 >Don't paste data like this to the list. Use ?dput to create an easy to use data.frame that users of the list can input with one R command. You will most likely get help very quickly at that point since our data will match your's exactly.> 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.I have not tested any of this, but I'm guessing something like the following would work. Assume your data.frame is called df. #NOT TESTED tmp <- apply(df, 1, function(x) sum(x == 0)) df$new_var <- ifelse(df$Out == 1, tmp, 0) See ?apply and ?ifelse .> > Best, Thomas > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Easy using rowSums :> x <- data.frame(X1=c(0,0,1),X2=c(1,1,1),X3=c(0,1,0))> x$Nulls <- rowSums(x==0) > xX1 X2 X3 Nulls 1 0 1 0 2 2 0 1 1 1 3 1 1 0 1 Cheers On Tue, Mar 30, 2010 at 6:31 PM, Thomas Jensen < thomas.jensen@eup.gess.ethz.ch> wrote:> 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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]