Hello all, i'm a newbie to R and now I get stuck by the question, would anybody help me please? Suppose, I got the following> test <- data.frame(test1=c(c(0.00, 0.005, 0.01, 0.015, 0.02))) > testtest1 1 0.000 2 0.005 3 0.010 4 0.015 5 0.020 Now I want another column inside test, named test2 for i=c(1:5), If test$test1[i] is 0, then the value of test$test2[i] is 1/test1[i] or else If test$test1[i] is not 0, then the value of test$test2[i] is 39 how should i programme in R to make it happen? thanks a lot! [[alternative HTML version deleted]]
Hello all, i'm a newbie to R and now I get stuck by the question, would anybody help me please? Suppose, I got the following> test <- data.frame(test1=c(c(0.00, 0.005, 0.01, 0.015, 0.02))) > testtest1 1 0.000 2 0.005 3 0.010 4 0.015 5 0.020 Now I want another column inside test, named test2 for i=c(1:5), If test$test1[i] is 0, then the value of test$test2[i] is 1/test1[i] or else If test$test1[i] is not 0, then the value of test$test2[i] is 39 how should i programme in R to make it happen? thanks a lot! [[alternative HTML version deleted]]
Hi, ?test$test2<-ifelse(test$test1!=0,39,1/test$test1) A.K. ----- Original Message ----- From: Yanyuan Zhu <yyz at tongji.edu.cn> To: r-help at r-project.org Cc: Sent: Thursday, December 20, 2012 10:45 AM Subject: [R] an entry level (stupid) question Hello all, i'm a newbie to R and now I get stuck by the question, would anybody help me please? Suppose, I got the following> test <- data.frame(test1=c(c(0.00, 0.005, 0.01, 0.015, 0.02))) > test? test1 1 0.000 2 0.005 3 0.010 4 0.015 5 0.020 Now I want another column inside test, named test2 for i=c(1:5), If test$test1[i] is 0, then the value of test$test2[i] is 1/test1[i] or else If test$test1[i] is not 0, then the value of test$test2[i] is 39 how should i programme in R to make it happen? thanks a lot! ??? [[alternative HTML version deleted]] ______________________________________________ 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.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Yanyuan Zhu > Sent: Thursday, December 20, 2012 7:48 AM > To: r-help at r-project.org > Subject: [R] an entry level (stupid) question > > Hello all, i'm a newbie to R and now I get stuck by the question, would > anybody help me please? > > Suppose, I got the following > > > test <- data.frame(test1=c(c(0.00, 0.005, 0.01, 0.015, 0.02))) > > test > test1 > 1 0.000 > 2 0.005 > 3 0.010 > 4 0.015 > 5 0.020 > > Now I want another column inside test, named test2 > for i=c(1:5), If test$test1[i] is 0, then the value of test$test2[i] is > 1/test1[i] > or else If test$test1[i] is not 0, then the value of test$test2[i] is > 39 > > how should i programme in R to make it happen? > > thanks a lot! >You probably want to use the ifelse() function, but your description of the problem change zeros into inf (i.e. infinity) and all other values into 39. Is that what you really want? Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Hi Yanyuan Check out: ?"==" ?"[" ?"<-" You should be able to assign the column quickly with no more than two lines of code using square brackets and assign. Or to literally use if, look at: ?"ifelse" Happy Christmas Chris Chris Campbell Tel. +44 (0) 1249 705 450?| Mobile. +44 (0) 7929 628349 mailto:ccampbell at mango-solutions.com?| http://www.mango-solutions.com Mango Solutions 2 Methuen Park Chippenham Wiltshire SN14 OGB UK -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Yanyuan Zhu Sent: 20 December 2012 15:48 To: r-help at r-project.org Subject: [R] an entry level (stupid) question Hello all, i'm a newbie to R and now I get stuck by the question, would anybody help me please? Suppose, I got the following> test <- data.frame(test1=c(c(0.00, 0.005, 0.01, 0.015, 0.02))) testtest1 1 0.000 2 0.005 3 0.010 4 0.015 5 0.020 Now I want another column inside test, named test2 for i=c(1:5), If test$test1[i] is 0, then the value of test$test2[i] is 1/test1[i] or else If test$test1[i] is not 0, then the value of test$test2[i] is 39 how should i programme in R to make it happen? thanks a lot! [[alternative HTML version deleted]] ______________________________________________ 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. -- LEGAL NOTICE\ \ This message is intended for the use of ...{{dropped:18}}
Hello, Inline. Em 20-12-2012 15:47, Yanyuan Zhu escreveu:> Hello all, i'm a newbie to R and now I get stuck by the question, would > anybody help me please? > > Suppose, I got the following > >> test <- data.frame(test1=c(c(0.00, 0.005, 0.01, 0.015, 0.02))) >> test > test1 > 1 0.000 > 2 0.005 > 3 0.010 > 4 0.015 > 5 0.020 > > Now I want another column inside test, named test2 > for i=c(1:5), If test$test1[i] is 0, then the value of test$test2[i] is > 1/test1[i]Note that this gives 1/0 == Inf. Anyway, a possibility is test$test2 <- 39 test$test2[test$test1 == 0] <- 1/test$test1[test$test1 == 0] test Hope this helps, Rui Barradas> or else If test$test1[i] is not 0, then the value of test$test2[i] is 39 > > how should i programme in R to make it happen? > > thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.