简体   繁体   中英

Need output in dataframe or need only 1 header for all rows of output in R

Below is dataset:

> head(u1spdf)

           coordinates A Latitude Longitude Altitude Date.No..Of.days            DateTime  geodist tdiff  
1  (116.3184, 39.9847) 1 39.98470  116.3184      492         39744.12 2008-10-23 02:53:04  0.00000     0  
2 (116.3184, 39.98468) 2 39.98468  116.3184      492         39744.12 2008-10-23 02:53:10  3.52000     6  
3 (116.3184, 39.98469) 3 39.98469  116.3184      492         39744.12 2008-10-23 02:53:15  2.83000     5  
4 (116.3184, 39.98469) 4 39.98469  116.3184      492         39744.12 2008-10-23 02:53:20  2.74000     5  
5 (116.3183, 39.98465) 5 39.98465  116.3183      492         39744.12 2008-10-23 02:53:25 11.03000     5  
6  (116.318, 39.98461) 6 39.98461  116.3180      493         39744.12 2008-10-23 02:53:30 20.81404     5

I need entries of which giodist is greater than 200m and tdiff is greater than 1200 sec

geodist is dist between 2 points and and tdiff is time diff between 2 points

I have executed below algorithm for this:

i <- 0 
pointNum <- nrow(u1spdf)  #the number of GPS points
while (i < pointNum) {
    j <- i+1; Token <- 0;
    while (j < pointNum) {
        cdist <- u1spdf$geodist[i+1]   #calculated the distance between points 
        #message("cdist: ",cdist)
        if (cdist > 200 | cdist ==0)  {
            ctime <- u1spdf$tdiff[i+1]  #calculated the time span between two points 
            #message("ctime: ",ctime)
            if (ctime > 1200 | ctime ==0 )  {
                print(u1spdf[i+1, ])
                i <- j
                Token <- 1 
            }
        }
        break

        j <- j+1
    }

    if (Token!=1) {    
        i <- i+1
    }

This algorithm have given below output:

              coordinates   A Latitude Longitude Altitude Date.No..Of.days            DateTime  geodist tdiff  
149  (116.2868, 39.99578) 149 39.99578  116.2868      716         39744.17 2008-10-23 04:08:07 1727.514  3772    
             coordinates   A Latitude Longitude Altitude Date.No..Of.days            DateTime  geodist tdiff   
471 (116.3202, 40.00478) 471 40.00478  116.3202      105          39744.4 2008-10-23 09:42:25 690.0795 18453

Need this output as 1 dataframe or its header is repeated everytime

could you please suggest how can I get above output as dataset

R is great for indexing objects with other objects...

meets_conditions = (u1spdf$geodist > 200 & u1spdf$tdiff > 1200)
your_desired_output = u1spdf[meets_conditions, ]

To provide an explanation, meets_conditions is a binary vector of same length as the number of rows in your data frame u1spdf . It indicates which rows satisfy the specified conditions. Subsequently, we can use it to indicate which rows we want to select from from the original data frame, to be assigned to a new data frame named your_desired_output , or you can just use u1spdf[meets_conditions, ] directly in other functions such as print(u1spdf[meets_conditions, ])

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