manish p
manish p

Reputation: 187

Filter dataframe based on rownames

Is there a way to filter dataframe based on rownames. For example shown below. First 3 rows are mandatory, then last 3 rows are conditions. So, there is false and negative values only, those should be displayed

    ColA
ABC FALSE
GDF 1
ASD True
dsa FALSE
gfd -1
poi TRUE

Expected Output (Only last row should be removed since it is true)

    ColA
ABC FALSE
GDF 1
ASD True
dsa FALSE
gfd -1
structure(list(col1 = structure(c(3L, 2L, 4L, 3L, 1L, 4L), .Label = c("-1", 
"1", "False", "True"), class = "factor")), .Names = "col1", row.names = c("ABC", 
"GDF", "ASD", "dsa", "gfd", "poi"), class = "data.frame")

Upvotes: 0

Views: 515

Answers (2)

akrun
akrun

Reputation: 887128

We could use filter

library(dplyr)
df1 %>%
     filter((!as.logical(col1))| row_number() <= 3)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

Maybe you can try :

mandatory_rows <- 3
subset(df, seq_len(nrow(df)) <= mandatory_rows | col1 != 'True')

#     col1
#ABC False
#GDF     1
#ASD  True
#dsa False
#gfd    -1

Upvotes: 1

Related Questions