Home > Net >  Extract Y values of 'double' item. R
Extract Y values of 'double' item. R

Time:06-07

I use co2 dataset to exemplify.

co2_dec <- decompose(co2)
co2_trend <- co2_dec$trend
typeof(co2_trend)

And I want to extract Y values of co2_trend item.

Thanks for your time.

CodePudding user response:

What you can do is make as.data.frame of the co2_trend. Then you have the values in a dataframe which are your "Y values" like this:

co2_dec <- decompose(co2)
co2_trend <- co2_dec$trend
co2_trend <- as.data.frame(co2_trend)
colnames(co2_trend) <- "trend"
co2_trend$trend

Output head(co2_trend, 15) :

      trend
1        NA
2        NA
3        NA
4        NA
5        NA
6        NA
7  315.8613
8  315.9175
9  315.9767
10 316.0696
11 316.1967
12 316.3287
13 316.4558
14 316.5687
15 316.6275

CodePudding user response:

I was reading the comments in your post and (if I understood you well) you already have what you want. You said you want all values from co2_trend and you can see that co2_trend is a double object.

typeof(co2_trend)
[1] "double"

In consequence, you can say (for example) co2_trend[1:10] and get the actual ten first numbers. Which in your case includes some NA's

co2_trend[1:10]
 [1]       NA       NA       NA       NA       NA       NA 315.8613 315.9175 315.9767 316.0696

If you don´t want the table-like structure of co2_trend, you can use:

co2_trend %>% as.vector

Which will give you all the numbers from start to end:

  [1]       NA       NA       NA       NA       NA       NA 315.8613 315.9175 315.9767 316.0696 316.1967 316.3287
 [13] 316.4558 316.5687 316.6275 316.6617 316.6900 316.7225 316.7667 316.8163 316.8867 316.9450 316.9863 317.0167
 [25] 317.0412 317.0954 317.1671 317.2633 317.3708 317.4508 317.5288 317.6083 317.6921 317.7862 317.8504 317.9033

Please let me know if this is not what you wanted so I can help you :)

CodePudding user response:

Seems already answered, but this is another option you have:

co2_dec <- decompose(co2)
co2_trend <- co2_dec$trend
typeof(co2_trend)

y <- co2_trend[1:length(co2_trend)]
dt <- data.frame(y)
  • Related