Run command for each column of a date.frame

Asked

Viewed 186 times

3

Hi, I have a data.frame (df) 8 rows x 8 columns. I am calling the columns "ST[i]".

df <- structure(list(ST1 = c(58.69, 58.5, 58.5, 58.69, 58.69, 
58.5, 58.69, 58.69), ST2 = c(68.7, 68.42, 68.42, 68.7, 
68.7, 68.42, 68.7, 68.7), ST3 = c(69.15, 69.15, 68.83, 
69.15, 69.15, 69.15, 69.15, 69.15), ST4 = c(78.99, 
80.29, 78.99, 77.7, 78.99, 80.29, 78.99, 77.7), ST5 = c(75.65, 
75.65, 75.65, 72.57, 71.07, 68.14, 66.7, 66.7), ST6 = c(71.35, 
71.35, 79.83, 86.38, 83.09, 87.49, 87.49, 78.76), ST7 = c(73.61, 
72.48, 78.22, 102.06, 82.33, 79.97, 112.48, 91.39), ST8 = c(77.57, 
77.57, 77.57, 77.57, 77.57, 77.57, 77.57, 79.17)), .Names = c("ST1", 
"ST2", "ST3", "ST4", "ST5", "ST6", "ST7", "ST8"), row.names = 122:129, class = "data.frame")

I need to execute a command waveslim::mra() for each of these columns. The result of this command, for each column, will be a list with 12 vectors. Therefore, the final product will be 8 lists, each list with 12 vectors.

I can do, for example:

dwc_ST1 <- mra(ST1, wf = "la8", method = "dwt", J=3, boundary = "reflection"
dwc_ST2 <- mra(ST2, wf = "la8", method = "dwt", J=3, boundary = "reflection")
...
dwc_ST8 <- mra(ST8, wf = "la8", method = "dwt", J=3, boundary = "reflection")

But it would be really nice if you simplified that. I thought about for(), in something like:

names_dwc <- as.character(rep(NA, ncol(df)))
for (k in 1:ncol(df)){
  names[k] <- paste('dwc_ST', k, sep = "")
  dwc_ST[k] <- mra(df[ ,k], wf = "la8", method = "dwt", J=3, boundary = "reflection")
  }

But it has appeared:

Error in ST[k] <- mra(df,[ , k], wf = "la8", : Object 'dwc_ST' not found

I thought I would have solved this problem by creating these 8 ST’s, with the line (I used above):

Names[k] <- Paste('dwc_ST', k, Sep = "")

I don’t seem to know how to create the files to generate the results. Someone can help me?

  • What is the expected result? Would it be 8 lists with 11 or 12 vectors or 1 list with 88 or 96 vectors? Also, it would be good for you to provide your data set, or a part of it (use the command dput), for your problem to be reproducible. Finally, when using functions that are not part of the R base, indicate at the beginning of your code which package is needed

  • Okay, thanks for the instructions, man.

  • The product will be 8 lists (one for each column) with 12 vectors each list. Total: 96 vectors. mra() is from the "waveslim" package. Anyway, the data.frame has 2048 lines. Using dput() I am pasting the first 8 lines.

  • To make it easier, put the result of dput in your question.

  • Rafael, I followed your instructions, deleted my comments and explained better in the question above. My original data have 2048 lines. I adapted to 8 lines.

2 answers

6


You can do this in a line with lapply:

dwc_ST <- lapply(df, mra, wf = "la8", method = "dwt", J=3, boundary = "reflection")

The result is a list of the calculations for each column:

str(dwc_ST, max.level = 1)
List of 8
 $ ST1:List of 4
 $ ST2:List of 4
 $ ST3:List of 4
 $ ST4:List of 4
 $ ST5:List of 4
 $ ST6:List of 4
 $ ST7:List of 4
 $ ST8:List of 4

If you make a point of renaming the list objects to dwc_ST1, dwc_ST2 etc.:

names(dwc_ST) <- paste0("dwc_", names(dwc_ST))

And if you do not want the objects in a list, and yes each of them separately on the desktop (which do not recommend), just use list2env:

list2env(dwc_ST, envir = globalenv()) # mas não recomendo
  • Thanks, Carlos! The result was great! As in my next step I will plot the combinations of the lists, and the vectors by list, I still don’t know if a list with more than one level is better for making these combinations and I should keep it big like this, or if I should separate it into several lists.

4

In your code, you create the object names_dwc and does not use it within the for. In the case names_dwc[k] <- paste('dwc_ST', k, sep = "") creates only one vector of characterand does not create the objects dwc_ST as you thought. For this, there is the function assign, which assigns a value to a name. Follows below code:

names_dwc <- as.character(rep(NA, ncol(df)))
for(k in 1:ncol(df)){
  names_dwc[k] <- paste('dwc_ST', k, sep = "")
  assign(names_dwc[k], mra(df[,k], wf = 'la8', method = 'dwt', J = 3, boundary = 'reflection'))
}
  • Xará, perfect guy! It seems that the problem was keeping the lists that were being created, and assign() was the solution, right? dwc_ST[k], before the for(). Thank you!

  • I’m glad the solution helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.