I make the same mistake all over again. In particular, suppose we have: a = c(1,2,3,4,5) and a variable that equals 1 for the elements I want to select: t = c(1,1,1,0,0) To select the first 3 elements. The problem is that a[t] would repeat the first element 3 times ..... I have to either convert `t` to boolean: a[t==1] Or use `which` a[which(t==1)] How can I "spot" this error? It often happens in long scripts. Do I have to check the type each time? Do you have any suggestions?
I practically never construct vectors like your `t` so it isn't a problem. And since I make a habit of verifying the types of all vectors I am using in expressions, if it did come up I would notice. On June 23, 2021 8:06:05 AM PDT, Phillips Rogfield <thebudget72 at gmail.com> wrote:>I make the same mistake all over again. > >In particular, suppose we have: > >a = c(1,2,3,4,5) > >and a variable that equals 1 for the elements I want to select: > >t = c(1,1,1,0,0) > >To select the first 3 elements. > >The problem is that > >a[t] > >would repeat the first element 3 times ..... > >I have to either convert `t` to boolean: > >a[t==1] > >Or use `which` > >a[which(t==1)] > >How can I "spot" this error? > >It often happens in long scripts. > >Do I have to check the type each time? > >Do you have any suggestions? > >______________________________________________ >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.-- Sent from my phone. Please excuse my brevity.
In my code, instead of 't', I name a vector of indices with a meaningful name, such as idxV, to make it obvious. Alternatively, a minor change in your style would be to replace your definition of t by t <- as.logical(c(1,1,1,0,0)) HTH, Eric On Wed, Jun 23, 2021 at 6:11 PM Phillips Rogfield <thebudget72 at gmail.com> wrote:> I make the same mistake all over again. > > In particular, suppose we have: > > a = c(1,2,3,4,5) > > and a variable that equals 1 for the elements I want to select: > > t = c(1,1,1,0,0) > > To select the first 3 elements. > > The problem is that > > a[t] > > would repeat the first element 3 times ..... > > I have to either convert `t` to boolean: > > a[t==1] > > Or use `which` > > a[which(t==1)] > > How can I "spot" this error? > > It often happens in long scripts. > > Do I have to check the type each time? > > Do you have any suggestions? > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
> a variable that equals 1 for the elements I want to select: > > t = c(1,1,1,0,0)How do you typically make such a variable? If you use something like t <- ifelse(x == "Yes", 1, 0) you should instead use t <- x == "Yes" Naming the variable something like 'isYes' instead of 't' might help as well. -Bill On Wed, Jun 23, 2021 at 8:11 AM Phillips Rogfield <thebudget72 at gmail.com> wrote:> I make the same mistake all over again. > > In particular, suppose we have: > > a = c(1,2,3,4,5) > > and a variable that equals 1 for the elements I want to select: > > t = c(1,1,1,0,0) > > To select the first 3 elements. > > The problem is that > > a[t] > > would repeat the first element 3 times ..... > > I have to either convert `t` to boolean: > > a[t==1] > > Or use `which` > > a[which(t==1)] > > How can I "spot" this error? > > It often happens in long scripts. > > Do I have to check the type each time? > > Do you have any suggestions? > > ______________________________________________ > 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. >[[alternative HTML version deleted]]