Efficiently transform multiple columns of a data frame

后端 未结 3 593
孤街浪徒
孤街浪徒 2020-12-17 00:10

I have a data frame, and I want to transform all columns (say, take the logs or whatever) with columns that match a certain name. So in the example below, I want to take the

相关标签:
3条回答
  • 2020-12-17 00:23
    df <- data.frame(
    Y = sample(0:1, 10, replace = TRUE),
    X.1 = sample(1:10),
    X.2 = sample(1:10),
    Z.1 = sample(151:160)
    )
    df
    

    assuming that you know those variables which requires conversions in the real dataframe (2 and 3 refers to the 2nd and 3rd variables in df which are X.1 and X.2)

    df2=log10(df[c(2:3)])
    df2
    

    if the variables are far a part in the dataframe you can select them like c(1,3,6,8:10,13) for 1st, 3rd, 6th 8 through 10 and 13th.this works only for numerical variables.

    0 讨论(0)
  • 2020-12-17 00:25
    vars <- c("X.1", "X.2")
    
    df[vars] <- lapply(df[vars], log)
    
    0 讨论(0)
  • 2020-12-17 00:35

    In the case of functions that will return a data.frame:

    cols <- c("X.1","X.2")
    df[cols] <- log(df[cols])
    

    Otherwise you will need to use lapply or a loop over the columns. These solutions will be slower than the solution above, so only use them if you must.

    df[cols] <- lapply(df[cols], function(x) c(NA,diff(x)))
    for(col in cols) {
      df[col] <- c(NA,diff(df[col]))
    }
    
    0 讨论(0)
提交回复
热议问题