简体   繁体   中英

how to write a for loop to loop through a column in R

I want to loop over a column. For each value of the column, there is a corresponded column in the test data. I want to find the corresponded column and copy the copy to a new column I just made. The follow is what I have done. But there are syntax error, because I don't know how to loop over a column properly using R.

  for (i in test$Position) {
    value1 <- test[i] 
    index <- which(names(test)== value1)
    res <- test[i,index]
    test$newCoulumn [i]=res

}

enter image description here

test <- structure(list(cc = c(90, 94, 87, 85, 86, 80, 92, 76, 90, 81, 
67), ST = c(90, 94, 87, 85, 86, 80, 92, 76, 90, 81, 67), RS = c(90, 
94, 87, 85, 86, 80, 92, 76, 90, 81, 67), LW = c(94, 92, 92, 90, 
92, 88, 91, 73, 86, 84, 64), LF = c(95, 93, 92, 90, 91, 87, 92, 
74, 89, 85, 65), CF = c(95, 93, 92, 90, 91, 87, 92, 74, 89, 85, 
65), RF = c(95, 93, 92, 90, 91, 87, 92, 74, 89, 85, 65), RW = c(94, 
92, 92, 90, 92, 88, 91, 73, 86, 84, 64), Position = c("RF", "ST", 
"LW", "RF", "LF", "RW", "RS", "CF", "ST", "cc", "RW")), row.names = c(NA, 
-11L), class = c("tbl_df", "tbl", "data.frame"))

test

    cc  ST  RS  LW  LF  CF  RF  RW  Position
    90  90  90  94  95  95  95  94  RF
    94  94  94  92  93  93  93  92  ST
    87  87  87  92  92  92  92  92  LW
    85  85  85  90  90  90  90  90  RF
    86  86  86  92  91  91  91  92  LF
    80  80  80  88  87  87  87  88  RW
    92  92  92  91  92  92  92  91  RS
    76  76  76  73  74  74  74  73  CF
    90  90  90  86  89  89  89  86  ST
    81  81  81  84  85  85  85  84  cc
    67  67  67  64  65  65  65  64  RW

I'm sure there must be a purrr::map2 version of this but I thought the task of sending two vectors (one the column of Position values and the other the row indices) to "[" was pretty straightforward. The only gotcha seemed to be that the mapply value needed to be unlisted. I think there might be a simplify2array argument that would do the same thing.

test$Pos_val <- unlist(mapply(function(x,y){test[y,x]}, x=test$Position, y=1:nrow(test)))
> test
# A tibble: 11 x 10
      cc    ST    RS    LW    LF    CF    RF    RW Position Pos_val
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>      <dbl>
 1    90    90    90    94    95    95    95    94 RF            95
 2    94    94    94    92    93    93    93    92 ST            94
 3    87    87    87    92    92    92    92    92 LW            92
 4    85    85    85    90    90    90    90    90 RF            90
 5    86    86    86    92    91    91    91    92 LF            91
 6    80    80    80    88    87    87    87    88 RW            88
 7    92    92    92    91    92    92    92    91 RS            92
 8    76    76    76    73    74    74    74    73 CF            74
 9    90    90    90    86    89    89    89    86 ST            90
10    81    81    81    84    85    85    85    84 cc            81
11    67    67    67    64    65    65    65    64 RW            64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM