I am attempting to filter data frames that have only one column. This results in a vector being returned like so:
single.c <- data.frame(col1=c(1,2,3,4,5), row.names=C("r1","r2","r3","r4","r5"))
single.c[single.c$col1 > 2,]
[1] 3 4 5
What I actually want is the data returned like it is for multi-column dataframes:
multi.c <- data.frame(col1=c(1,2,3,4,5), col2=c(1,2,3,4,5), row.names=c("r1","r2","r3","r4","r5"))
multi.c[multi.c$col2 > 2,]
col1 col2
r3 3 3
r4 4 4
r5 5 5
I can see it makes sense to return a vector if there are no other columns, but generally I want see what rows have given that result too. Why does this happen, and is there an easy way to keep the data frame shape in the result, including the rownames?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…