简体   繁体   中英

Remove rows from an R Data frame

I have a data set that has a number of columns, but to keep it short here's an abbreviated form (the data is from the Divvy competition)

Trip ID       Tripduration     from_id      to_id
1               50               2            2
2               700              2            5
3               80               2            4

When I imported the data from the .csv R made it into a data.frame, which is OK. So using

full.set2<-sapply(full.set, function(x) 
                            if(is.factor(x)){
                                             as.numeric(x)
                                             }else
                                             {
                                             x
                                             })

I was able to convert the entire thing into a "Large Matrix" (according to RStudio). So Now I'm trying to clear out the values that meet 2 criteria:
1) Tripduration <= 90
&&
2) from_id == to_id

When I do

full.set2t<-full.set2[full.set2[,2]>=90]

It makes full.set2t into one very large vector rather than keeping it as a matrix (though it does look like it might be removing the proper values, as the number of elements decreased).

I've also tried subset on the original data.frame but I got the error that "> not meaningful for factors"

Any ideas? I've searched around and can't seem to get any of the other solutions I'v efound to work

EDIT: As I'm continuing searching I'll put here other things I've tried that didn't work:

x<-seq(1:90)
x<-as.numeric(x)
y<- full.set[! full.set$tripduration %in% x,]
## Does something, removes some data points but not all of the proper ones

Solution found!

 full.set$tripduration<-as.numeric(full.set$tripduration)
 full.set.test<-full.set[full.set$tripduration>90]

Turns out that the column was a factor and not numeric, and I didn't know how to convert that single column

The problem is this line

full.set2t<-full.set2[full.set2[,2]>=90]

To subset a data.frame you need to use [rows,columns], where leaving one blank means select eveything. So the line should be

full.set2t<-full.set2[full.set2[,2]>=90,] # note the comma

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM