Get only the value of the element in the data frame R (no index)

I have a small data frame:

> testdfcompound_1 E1 E2 2012-05-17 01:00:58 20.94700 2.148299 2012-05-17 01:01:57 15.36875 2.199166 2012-05-17 01:02:56 19.05800 2.697803 2012-05-17 01:03:55 17.90500 2.358735 

And I want to get only the E1 value for the first element, therefore, 20.94700.

But I can’t find a way to get it. If I try: testdfcompound_1$E1[1] , I get:

 > testdfcompound_1$E1[1] E1 2012-05-17 01:00:58 20.947 

How do I get only 20.947?

+6
source share
3 answers

Double square brackets.

 d1 <- data.frame(a=1:5, b=rnorm(5)) d1$b[[3]] 

That should do it. But there are many ways to do this ...

+15
source
 as.double( testdfcompound_1$E1[1]) 

Then you get the value. Was β€œ2012-05-17 01:00:58” and β€œE1” not the name of the row and column in your case?

+3
source

I find it a little puzzling. What does str(testdfcompound_1) show? If you have a data frame, your subset should work.

A small example that creates a data frame that is similar to your sample data and for which a subset works.

 set.seed(123) df <- data.frame(E1 = rnorm(5) + 20, E2 = rnorm(5) + 2) df rownames(df) <- Sys.time() + 1:5 df # looks pretty similar df$E1[1] # returns the number only. 
0
source

Source: https://habr.com/ru/post/952729/


All Articles