5

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.....

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your comments. Isn't there any thing without loop
Great. This is what i was looking forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.