Rename columns in multiple dataframes, R

前端 未结 3 1662
滥情空心
滥情空心 2020-12-16 19:34

I am trying to rename columns of multiple data.frames.

To give an example, let\'s say I\'ve a list of data.frames dfA, d

3条回答
  •  生来不讨喜
    2020-12-16 19:47

    There are two things here:

    • 1) You should return the value you want from your function. Else, the last value will be returned. In your case, that's names(x). So, instead you should add as the final line, return(x) or simply x. So, your function would look like:

      ChangeNames <- function(x) {
          names(x) <- c("A", "B", "C" )
          return(x)
      }
      
    • 2) lapply does not modify your input objects by reference. It works on a copy. So, you'll have to assign the results back. Or another alternative is to use for-loops instead of lapply:

      # option 1
      dfs <- lapply(dfs, ChangeNames)
      
      # option 2
      for (i in seq_along(dfs)) {
          names(dfs[[i]]) <- c("A", "B", "C")
      }
      

    Even using the for-loop, you'll still make a copy (because names(.) <- . does). You can verify this by using tracemem.

    df <- data.frame(x=1:5, y=6:10, z=11:15)
    tracemem(df)
    # [1] "<0x7f98ec24a480>"
    names(df) <- c("A", "B", "C")
    tracemem(df)
    # [1] "<0x7f98e7f9e318>"
    

    If you want to modify by reference, you can use data.table package's setnames function:

    df <- data.frame(x=1:5, y=6:10, z=11:15)
    require(data.table)
    tracemem(df)
    # [1] "<0x7f98ec76d7b0>"
    setnames(df, c("A", "B", "C"))
    tracemem(df)
    # [1] "<0x7f98ec76d7b0>"
    

    You see that the memory location df is mapped to hasn't changed. The names have been modified by reference.

提交回复
热议问题