Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop columns in a nested data frame in R?

Tags:

dataframe

r

As i know there are different ways to drop a column of a data frame in R like

Drop data frame columns by name

How to drop columns by name in a data frame

Remove an entire column from a data.frame in R

Now My question: I have a data frame say df having 400 plus files in the form of nested data frame.

        df
    [[1]]
                SignalIntensity SNR
        1   109 6.1823089314    0.8453576915
        2   110 10.1727771385   4.3837077591
        3   111 7.2922746927    1.0725751161
        4   112 8.8984671629    2.3192184908
        5   113 9.5910338232    3.7133402249
        6   114 7.9850187685    1.5008899345
        7   116 7.7893230124    1.3636655582
                       .
                       .
                       .
    [[2]]

            SignalIntensity SNR
    1   109 6.1823089314    0.8453576915
    2   110 10.1727771385   4.3837077591
    3   111 7.2922746927    1.0725751161
    4   112 8.8984671629    2.3192184908
    5   113 9.5910338232    3.7133402249
    6   114 7.9850187685    1.5008899345
    7   116 7.7893230124    1.3636655582
                  .
                  .
                  .
[[3]]

        ID  SignalIntensity SNR
    1   109 6.1823089314    0.8453576915
    2   110 10.1727771385   4.3837077591
    3   111 7.2922746927    1.0725751161
    4   112 8.8984671629    2.3192184908
    5   113 9.5910338232    3.7133402249
    6   114 7.9850187685    1.5008899345
    7   116 7.7893230124    1.3636655582
                   .
                   .
                   .
 and so on.....

I want to remove column 1 from all the 400 plus files. The column 1 header may be present or absent.

I know to use df[[1]][,-1] to remove column 1 of first file. In order to do for all the files i have to repeat it 400 or so times to make the work done. There may be 1 or 2 line of code in R to do this. How?? help appreciated.

Final data frame is expected to be like

df
     [[1]]
                    SignalIntensity SNR
            1       6.1823089314    0.8453576915
            2       10.1727771385   4.3837077591
            3       7.2922746927    1.0725751161
            4       8.8984671629    2.3192184908
            5       9.5910338232    3.7133402249
            6       7.9850187685    1.5008899345
            7       7.7893230124    1.3636655582
                           .
                           .
                           .
        [[2]]

                SignalIntensity SNR
        1       6.1823089314    0.8453576915
        2       10.1727771385   4.3837077591
        3       7.2922746927    1.0725751161
        4       8.8984671629    2.3192184908
        5       9.5910338232    3.7133402249
        6       7.9850187685    1.5008899345
        7       7.7893230124    1.3636655582
                      .
                      .
                      .
    [[3]]

                SignalIntensity SNR
        1       6.1823089314    0.8453576915
        2       10.1727771385   4.3837077591
        3       7.2922746927    1.0725751161
        4       8.8984671629    2.3192184908
        5       9.5910338232    3.7133402249
        6       7.9850187685    1.5008899345
        7       7.7893230124    1.3636655582
                       .
                       .
                       .
     and so on.....
like image 218
Agaz Wani Avatar asked Mar 14 '15 07:03

Agaz Wani


People also ask

How to drop one or multiple columns in R data frame?

Here are multiple ways how to drop one or multiple columns in the R data frame. You can use R base functionality or package like dplyr, but it is not the most time-consuming operation. By knowing how to do that in different ways, you can choose a suitable approach.

How do I remove a column from a Dataframe in R?

How to Drop Columns from Data Frame in R (With Examples) The easiest way to drop columns from a data frame in R is to use the subset () function, which uses the following basic syntax: #remove columns var1 and var3 new_df <- subset (df, select = -c (var1, var3))

How to drop a variable from a Dataframe?

The parameter "data" refers to input data frame. "cols" refer to the variables you want to keep / remove. "newdata" refers to the output data frame. To drop variables, use the code below. The drop = 1 implies removing variables which are defined in the second parameter of the function.

How to drop a column in R using dplyr?

The dplyr is an R package for performing common data manipulation tasks. The select () function of dplyr is designed to select columns from a data frame. The ! operator is used to take the complement of a set of variables. It will help us drop columns using the select () function.


1 Answers

One option is with a loop:

for(i in seq_along(df)) {
  df[[i]] <- df[[i]][-1]
}

Or without the loop, using lapply:

df2 <- lapply(df, subset, select=-1)
like image 123
Dominic Comtois Avatar answered Sep 28 '22 09:09

Dominic Comtois