Home > other >  Structure of a for loop
Structure of a for loop

Time:02-01

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.

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"))

CodePudding user response:

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.

CodePudding user response:

We do not need a for loop here:

df[df==123]<-333

If we really need for loops:

for(i in 1:ncol(df)){
    df[i][df[i]==123] <-333
 }

output

df

# A tibble: 3 x 4
   sec1  sec2  sec3  sec4
  <dbl> <dbl> <dbl> <dbl>
1     1   333   333     1
2   333     1     0   333
3     1     1     0     1

CodePudding user response:

in addition to DaveArmstrong Answer this would work for all rows and columns:

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)){
  
  for(j in 1:ncol(dat)){
  
  dat[i,j] <- ifelse(dat[i,j] == 123, 333, dat[i,j])
  
    }
  }
  
  •  Tags:  
  • Related