Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Structure of a for loop

I am learning how to create a function in R, but I am struggling to understand how to write for loop. My understanding is that

for (item I list_items) {

do_something(itemn)

}

I would like to write a for loop to replace with 333 the cells that are equal with 123. So the item is 123 and the list of items is the df from sec1 till sec4.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could somebody explain this to me, please? And how this can be included in a function?

Sample code:

structure(list(sec1 = c(1, 123, 1), sec2 = c(123, 1, 1), sec3 = c(123, 
0, 0), sec4 = c(1, 123, 1)), spec = structure(list(cols = list(
    sec1 = structure(list(), class = c("collector_double", "collector"
    )), sec2 = structure(list(), class = c("collector_double", 
    "collector")), sec3 = structure(list(), class = c("collector_double", 
    "collector")), sec4 = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"),  row.names = c(NA, 
-3L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

>Solution :

Here’s how it would work for one column of your data:

dat <- structure(list(sec1 = c(1, 123, 1), 
                      sec2 = c(123, 1, 1), 
                      sec3 = c(123, 0, 0), 
                      sec4 = c(1, 123, 1)), 
                 spec = structure(list(cols = list(                                                                              
                      sec1 = structure(list(),   
                      class = c("collector_double", "collector")),   
                      sec2 = structure(list(), 
                      class = c("collector_double","collector")),
                      sec3 = structure(list(), 
                      class = c("collector_double", "collector")), 
                      sec4 = structure(list(), 
                      class = c("collector_double","collector"))), 
                 default = structure(list(), 
                      class = c("collector_guess","collector")), 
                      delim = ","), class = "col_spec"),  
                      row.names = c(NA,-3L), class = 
                 c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))


for(i in 1:nrow(dat)){
  dat$sec1[i] <- ifelse(dat$sec1[i] == 123, 333, dat$sec1[i])
}
dat
#>   sec1 sec2 sec3 sec4
#> 1    1  123  123    1
#> 2  333    1    0  123
#> 3    1    1    0    1

Created on 2022-01-31 by the reprex package (v2.0.1)

To replace all of them, using for loops, you could do a double loop over columns and rows.

for(j in names(dat)){
  for(i in 1:nrow(dat)){
    dat[[j]][i] <- ifelse(dat[[j]][i] == 123, 333, dat[[j]][i])
  }
}

Of course, as others have identified, you certainly don’t need a for loop to accomplish this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading