TransWikia.com

Getting names and numbers in specific vectors from a dataframe in a loop R

Geographic Information Systems Asked by Kledson Lemes on July 14, 2021

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




        ####code
   
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 
      
      print(paste0("control","_",i,"_",k))
    
    ### save the pixel value 
    
    Pixel_value<- s
    
    print(Pixel_value)
    

}
}
}

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.

2 Answers

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")
  }      
  print(cb)

Correct answer by Jeffrey Evans on July 14, 2021

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

Answered by nmpeterson on July 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP