Efficiently transform multiple columns of a data frame

后端 未结 3 601
孤街浪徒
孤街浪徒 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.

提交回复
热议问题