Jan Stamm
Jan Stamm

Reputation: 15

Select specific value out of a data frame

I want to select a specific value from a data frame and can't figure out how. Here's the data frame:

picture of data frame

I want to select a specific value from the column "X1", but I don't want to access it through the index (1 to 10), but by using the "time"-column.

If I wanted to select the value X1 from row 8 (0.89) through the index, I could do this:

df[8,"X1"]

But I want to access the value through the Date & Time. I didn't find a solution yet. The time-column is basically the column X and X.1 merged into one separate column with a POSIXct format (%Y-%m-%d %H:%M:%S).

Something like this (but doesn't work like that):

df[df$time[2018-08-10 13:29:20], "X1"]

I would be thankful for any ideas.

Upvotes: 1

Views: 20119

Answers (1)

floe
floe

Reputation: 417

It's very easy. You have just to do a comparision like

 df[df$time == "2018-08-10 13:29:20", "X1"]

if you want to have all values of "X1" greater than a specific time you can even go with lager or lower comparisions like

 df[df$time > "2018-08-10 13:29:20", "X1"]    
 df[df$time >= "2018-08-10 13:29:20", "X1"]    
 df[df$time < "2018-08-10 13:29:20", "X1"]    
 df[df$time <= "2018-08-10 13:29:20", "X1"]    

The comparision will create a logic vector which gives you all rows back where the logic vector is TRUE

If it is not working properly you have to be shure your time-comlumn is in the right format like POSIXct. You can do it with as.POSIXct

Upvotes: 3

Related Questions