I have a huge DataFrame, in which I need to extract some information that generates filenames that I need to search in my working directory later, I also need the value in each cell of the pixel column. For this I created a loop, in which I wanted to search for these pixel names and values based on the name of the columns.

Just like in the example and code below:

enter image description here

Replicable example

  ###Create a DataFrame 
  pixel_tot<- data.frame (name= c ("Area A",  "Area B",  "Area C", "Area D", "Area E", "Area F",  "Area G", "Area H ", "Area I", 
                                    "Area J", "Area K",  "Area L",  "Area M", "Area N ", "Area O","Area P"),

            AP= c("PI", "PI", "PI", "PI", "UN", 
                  "UN", "UN", "UN", "US", "US", "US","US", 
                  "TI", "TI", "TI", "TI"),
            AW= c("wood", "wood", "arc",  "arc","wood", "wood", "arc",  "arc", 
                  "wood", "wood", "arc",  "arc","wood", "wood", "arc",  "arc"),
             DIST=c("km0",  "km10", "km0",  "km10","km0",  "km10", "km0", "km10","km0",  "km10", 
                    "km0",  "km10","km0",  "km10", "km0",  "km10"),
            pixels= c(598, 234, 789, 546, 8956, 4454, 7989, 
                      789, 7866, 454, 7846, 968, 5847, 14758, 4578, 5864))

for (k in pixel_tot[,"AW"]) {
  for (i in pixel_tot[,"AP"]) {
    for (s in pixel_tot[,"pixels"]) {
      ## I need to get this information about ( AW and AP) to generate a file name that
      ##I need to call and open, and joining these columns generates that name 
    ### save the pixel value 
    Pixel_value<- s


This code does not generate any error, but I expected it to form only 16 combinations, but this is not the case. They generate thousands of different combinations.


So, I am on board with @nmpeterson answer, it is simple and elegant. However, you appear to have multiple values returned given a specific AP & AW combination. Are you wanting each pixel total for the multiple values or an aggregation of them?

If you look at the unique combinations, in your example data.frame you have n=8, with n=16 representing the repeated combinations.

unique(paste0(pixel_tot$AP, "_", pixel_tot$AW))

To aggregate, you can use expand.grid to create all unique combinations. Note, I defactor the resulting data.frame object elsewise, it creates headaches. I also add a column to hold the sums.

( cb = expand.grid(unique(pixel_tot[,"AP"]), unique(pixel_tot[,"AW"])) )
  names(cb) <- c("AP","AW")
    cb[1:2] <- lapply(cb[1:2], as.character)
      cb$pixel_sum <- NA

Now, a simple for loop (or any other variant you wish to implement) that creates the sums for the unique combinations.

  for(i in 1:nrow(cb)) {
    s <- sum(pixel_tot[pixel_tot$AP == as.character(cb[i,][1]) | 
             pixel_tot$AW == as.character(cb[i,][2]),]$pixels)
    cb[i,][3] <- s
    cat(paste0(as.character(cb[i,][1]),"_",as.character(cb[i,][2]), "_", s), "\n")
  • thank you very much it worked perfectly!! Mar 3 '21 at 20:25

Instead of using a bunch of loops, take advantage of R's vector processing.

pixel_tot$filename <- paste0("control_", pixel_tot$AP, "_", pixel_tot$AW)
print(pixel_tot[c("filename", "pixels")])
  • In fact, I will need to use each combination separately, to call shapefile files, each one at a time, overlay a raster and randomly extract the amount of pixels corresponding to the pixels column. Apparently, that way it will not be possible. I removed this part of the code because I thought it might be confusing. Mar 2 '21 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.