Dear R-users I have a problem in the code below, the problem is because i want to change the variable BZ which is a vector with 200x1 dimension to the var. W which is categorical variable with same dimension of BZ but with categorical values with 4 categories but i got on only zero values in w why? any help would be appreciated. N<-200;P<-9 #Sample size BZ=numeric(N) W<-numeric(N) for (t in 1:1) { #Generate the data for the simulation study for (i in 1:N) { #transform theta to ordered categorical variables for(j in 1:1){ if(BZ[j] < 0.25){ W[j] = 1 }else if(BZ[j] >=0.25 & BZ[j] < 0.5){ W[j] = 2 }else if(BZ[j] >=0.5 & BZ[j] < 0.75){ W[j] = 3 }else{ W[j] = 4 }} #Input data set data<-list(N=200,P=9,Q=W) }} #end W Regards -- Thanoon Y. Thanoon PhD Candidate Department of Mathematical Sciences Faculty of Science University Technology Malaysia, UTM E.Mail: Thanoon.younis80 at gmail.com E.Mail: dawn_prayer80 at yahoo.com Facebook:Thanoon Younis AL-Shakerchy Twitter: Thanoon Alshakerchy H.P:00601127550205 [[alternative HTML version deleted]]
Why don't you just multiply by four, round, and add one? B. On Nov 3, 2015, at 10:02 PM, thanoon younis <thanoon.younis80 at gmail.com> wrote:> Dear R-users > > I have a problem in the code below, the problem is because i want to change > the variable BZ which is a vector with 200x1 dimension to the var. W which > is categorical variable with same dimension of BZ but with categorical > values with 4 categories but i got on only zero values in w why? any help > would be appreciated. > > N<-200;P<-9 #Sample size > BZ=numeric(N) > > W<-numeric(N) > > for (t in 1:1) { > #Generate the data for the simulation study > for (i in 1:N) { > > #transform theta to ordered categorical variables > > > for(j in 1:1){ > if(BZ[j] < 0.25){ > W[j] = 1 > }else if(BZ[j] >=0.25 & BZ[j] < 0.5){ > W[j] = 2 > }else if(BZ[j] >=0.5 & BZ[j] < 0.75){ > W[j] = 3 > }else{ > W[j] = 4 > }} > > > #Input data set > data<-list(N=200,P=9,Q=W) > > }} #end > > W > > > > > Regards > > > -- > Thanoon Y. Thanoon > PhD Candidate > Department of Mathematical Sciences > Faculty of Science > University Technology Malaysia, UTM > E.Mail: Thanoon.younis80 at gmail.com > E.Mail: dawn_prayer80 at yahoo.com > Facebook:Thanoon Younis AL-Shakerchy > Twitter: Thanoon Alshakerchy > H.P:00601127550205 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
I have not mustered the stamina to go through your code in detail, but it looks to me like the "1:1" constructions are typos and should perhaps be "1:N". Aside from that, I conjecture that what you want to accomplish could be very simply done with the cut() function. This is not entirely clear since it is not really clear what you want to accomplish. Note that you appear to evince a misunderstanding of R data structures; a vector of length 200 is subtly different from a 200 x 1 array. I think you should study up on some basic R (e.g. read "An Introduction to R") before proceeding further. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 On 04/11/15 16:02, thanoon younis wrote:> Dear R-users > > I have a problem in the code below, the problem is because i want to change > the variable BZ which is a vector with 200x1 dimension to the var. W which > is categorical variable with same dimension of BZ but with categorical > values with 4 categories but i got on only zero values in w why? any help > would be appreciated. > > N<-200;P<-9 #Sample size > BZ=numeric(N) > > W<-numeric(N) > > for (t in 1:1) { > #Generate the data for the simulation study > for (i in 1:N) { > > #transform theta to ordered categorical variables > > > for(j in 1:1){ > if(BZ[j] < 0.25){ > W[j] = 1 > }else if(BZ[j] >=0.25 & BZ[j] < 0.5){ > W[j] = 2 > }else if(BZ[j] >=0.5 & BZ[j] < 0.75){ > W[j] = 3 > }else{ > W[j] = 4 > }} > > > #Input data set > data<-list(N=200,P=9,Q=W) > > }} #end
Sorry, not round. floor() B. On Nov 3, 2015, at 10:35 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:> Why don't you just multiply by four, round, and add one? > > > > B. > On Nov 3, 2015, at 10:02 PM, thanoon younis <thanoon.younis80 at gmail.com> wrote: > >> Dear R-users >> >> I have a problem in the code below, the problem is because i want to change >> the variable BZ which is a vector with 200x1 dimension to the var. W which >> is categorical variable with same dimension of BZ but with categorical >> values with 4 categories but i got on only zero values in w why? any help >> would be appreciated. >> >> N<-200;P<-9 #Sample size >> BZ=numeric(N) >> >> W<-numeric(N) >> >> for (t in 1:1) { >> #Generate the data for the simulation study >> for (i in 1:N) { >> >> #transform theta to ordered categorical variables >> >> >> for(j in 1:1){ >> if(BZ[j] < 0.25){ >> W[j] = 1 >> }else if(BZ[j] >=0.25 & BZ[j] < 0.5){ >> W[j] = 2 >> }else if(BZ[j] >=0.5 & BZ[j] < 0.75){ >> W[j] = 3 >> }else{ >> W[j] = 4 >> }} >> >> >> #Input data set >> data<-list(N=200,P=9,Q=W) >> >> }} #end >> >> W >> >> >> >> >> Regards >> >> >> -- >> Thanoon Y. Thanoon >> PhD Candidate >> Department of Mathematical Sciences >> Faculty of Science >> University Technology Malaysia, UTM >> E.Mail: Thanoon.younis80 at gmail.com >> E.Mail: dawn_prayer80 at yahoo.com >> Facebook:Thanoon Younis AL-Shakerchy >> Twitter: Thanoon Alshakerchy >> H.P:00601127550205 >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >
Thank you for your response I have a vector of unknown values BZ which is defined already BZ=numeric(N) and i want to transfer BZ to ordered categorical variable with 4 categories W but when i write this code for(j in 1:1){ if(BZ[j] < 0.25){ W[j] = 1 }else if(BZ[j] >=0.25 & BZ[j] < 0.5){ W[j] = 2 }else if(BZ[j] >=0.5 & BZ[j] < 0.75){ W[j] = 3 }else{ W[j] = 4 }} i found the values of W are only 0, but if i change for(j in 1:N) the values of W will be only 1. how can i solve this problem? Regards On 4 November 2015 at 06:35, Boris Steipe <boris.steipe at utoronto.ca> wrote:> Why don't you just multiply by four, round, and add one? > > > > B. > On Nov 3, 2015, at 10:02 PM, thanoon younis <thanoon.younis80 at gmail.com> > wrote: > > > Dear R-users > > > > I have a problem in the code below, the problem is because i want to > change > > the variable BZ which is a vector with 200x1 dimension to the var. W > which > > is categorical variable with same dimension of BZ but with categorical > > values with 4 categories but i got on only zero values in w why? any > help > > would be appreciated. > > > > N<-200;P<-9 #Sample size > > BZ=numeric(N) > > > > W<-numeric(N) > > > > for (t in 1:1) { > > #Generate the data for the simulation study > > for (i in 1:N) { > > > > #transform theta to ordered categorical variables > > > > > > for(j in 1:1){ > > if(BZ[j] < 0.25){ > > W[j] = 1 > > }else if(BZ[j] >=0.25 & BZ[j] < 0.5){ > > W[j] = 2 > > }else if(BZ[j] >=0.5 & BZ[j] < 0.75){ > > W[j] = 3 > > }else{ > > W[j] = 4 > > }} > > > > > > #Input data set > > data<-list(N=200,P=9,Q=W) > > > > }} #end > > > > W > > > > > > > > > > Regards > > > > > > -- > > Thanoon Y. Thanoon > > PhD Candidate > > Department of Mathematical Sciences > > Faculty of Science > > University Technology Malaysia, UTM > > E.Mail: Thanoon.younis80 at gmail.com > > E.Mail: dawn_prayer80 at yahoo.com > > Facebook:Thanoon Younis AL-Shakerchy > > Twitter: Thanoon Alshakerchy > > H.P:00601127550205 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > >-- Thanoon Y. Thanoon PhD Candidate Department of Mathematical Sciences Faculty of Science University Technology Malaysia, UTM E.Mail: Thanoon.younis80 at gmail.com E.Mail: dawn_prayer80 at yahoo.com Facebook:Thanoon Younis AL-Shakerchy Twitter: Thanoon Alshakerchy H.P:00601127550205 [[alternative HTML version deleted]]