Hi:

Wouldn't ifelse() work here?

tco <- with(df, ifelse(TargetColor == 'B', CannonOriB, CannonOriR))

ifelse() is vectorized, so there should be no need for a loop.

Test:
> df <- data.table(TargetColor = c('B', 'R'), CannonOriB = c(5, 5),
+                  CannonOriR = c(3, 3), stringsAsFactors = FALSE)
> with(df, ifelse(TargetColor == 'B', CannonOriB, CannonOriR))
[1] 5 3

HTH,
Dennis

On Mon, Feb 14, 2011 at 1:02 AM, Frank Tamborello <
franklin.tamborello@uth.tmc.edu> wrote:

> Dear R-Help,
>
> I am trying to compute a new variable, let's call it "target cannon
> orientation (tco)" based conditionally on old variables, "TargetColor,"
> "CannonOriB," and "CannonOriR." For every case in the data set, if
> TargetColor is "B" then I want tco to equal the value for that case of
> CannonOirB, else CannonOriR. I've tried writing for loops and functions that
> I can feed to sapply. I suspect there must be a simple solution but I cannot
> seem to get either incantation to perform the assignment. What would be a
> good way to do this?
>
> Example data:
> TargetColor.1.18     CannonOriB.1.18     "CannonOriR.1.1
> "B"     5       3
> "R"     5       3
>
>
> Example assignment of tco
> "tco"
> 5
> 3
>
> Thanks much!
>
> Frank Tamborello, PhD
> W. M. Keck Postdoctoral Fellow
> School of Biomedical Informatics
> University of Texas Health Science Center, Houston
> https://xfiles.uth.tmc.edu/Users/ftamborello/public/index.html
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]

