[R] How to get rid of my for loop

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon Sep 29 13:08:50 CEST 2014


Dear Barry,

You have to rethink the input format. This is easy if you use a matrix.

A <- cbind(A_01, A_02, A_03, A_04)
index <- cbind(seq_along(VFD_ID), VFD_ID)
A[index]

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey


-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Barry King
Verzonden: maandag 29 september 2014 11:59
Aan: r-help op r-project.org
Onderwerp: [R] How to get rid of my for loop

# Here are sample data, sample vectors, and a for loop # that I am using now. I wish to get rid of the for loop # and use functions and one of the apply functions to # perform the work without needing a many iteration for loop.
A_01 <- 1:5
A_02 <- 6:10
A_03 <- 11:15
A_04 <- 16:20

B_01 <- 101:105
B_02 <- 106:110
B_03 <- 111:115
B_04 <- 116:120

# I am showing just two pairs, A_x with A_array and B_x with B_array.
# In actuality there are about a dozen such pairings.
A_array <- array(NA, dim=5)
B_array <- array(NA, dim=5)

VFD_ID <- c(3, 2, NA, 1, 3)
nobs <- length(VFD_ID)

# This for loop works fine but is not appropriate for # a large number of observations in VFD_ID.
for (i in 1:nobs){
  if (is.na(VFD_ID[i])){
    A_array[i]<- 0
    B_array[i]<- 0
  } else if (VFD_ID[i] == 1){
    A_array[i]<-A_01[i]
    B_array[i]<-B_01[i]
  } else if (VFD_ID[i] == 2){
    A_array[i]<-A_02[i]
    B_array[i]<-B_02[i]
  } else if (VFD_ID[i] == 3){
    A_array[i]<-A_03[i]
    B_array[i]<-B_03[i]
  } else if (VFD_ID[i] == 4){
    A_array[i]<-A_04[i]
    B_array[i]<-B_04[i]
  }
}

# This does NOT work. It returns the entire vector, # not just the corresponding element in VFD_ID.
# (Note: It seems like a switch function would work here # but I was unable to get it to work correctly.) A_array_fnc <- function(x){
  if (is.na(x)) return (0) else
    if (x == 1) return (A_01) else
      if (x == 2) return (A_02) else
        if (x == 3) return (A_03) else
          if (x == 4) return (A_04)
}

B_array_fnc <- function(x){
  if (is.na(x)) return (0) else
    if (x == 1) return (B_01) else
      if (x == 2) return (B_02) else
        if (x == 3) return (B_03) else
          if (x == 4) return (B_04)
}

A_array <- sapply(VFD_ID,A_array_fnc)
B_array <- sapply(VFD_ID,B_array_fnc)

# Is there a way to write the functions correctly so that # the return value for, say A_array[4] is A_03[4] if VFD_ID == 3?
# Any assistance is greatly appreciated.

______________________________________________
R-help op 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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-help mailing list