Extracting rows from a data.frame

后端 未结 2 927
-上瘾入骨i
-上瘾入骨i 2021-01-21 16:26

I have a n by m data.frame where column 1 has the information of interest. I want to create sub data.frames based upon what the value in a row of colum

相关标签:
2条回答
  • 2021-01-21 17:03

    Instead of :

    s <- data.frame(df1$P = S)
    

    try:

    s <- data.frame(df1[df1$P == S,])
    

    or

    s <- data.frame(df1[df1$P == 'S',])
    

    if you want to control number of rows try:

    s <- data.frame(df1[df1$P == 'S',1:5])
    
    0 讨论(0)
  • 2021-01-21 17:13

    Breaking your data into a list of data.frames using split may also work here, and prevent cluttering your workspace. E.g:

    df1 <- data.frame(P=c("S","S","A","I"),data=rep(1,4))
    df1
    
    #  P data
    #1 S    1
    #2 S    1
    #3 A    1
    #4 I    1
    
    result <- split(df1,df1$P)
    
    #$A
    #  P data
    #3 A    1
    #
    #$I
    #  P data
    #4 I    1
    #
    #$S
    #  P data
    #1 S    1
    #2 S    1
    

    You can then access the parts of the list like:

    result$S
    

    or

    result[["S"]]
    

    Voila:

    #  P data
    #1 S    1
    #2 S    1
    
    0 讨论(0)
提交回复
热议问题