user12167116
user12167116

Reputation: 31

Converting data objects in R

I have a data object called dat that looks like this:

> dat


          PC1          PC2          PC3          PC4          PC5 
 6.403056e-04 5.433502e-04 7.888467e-05 6.776798e-05 2.589316e-05 

And I want to convert dat into this:

> dat
 [1] 6.403056e-04 5.433502e-04 7.888467e-05 6.776798e-05
 [5] 2.589316e-05

How can I do this in R?

Upvotes: 0

Views: 67

Answers (2)

Phil
Phil

Reputation: 185

As others pointed out, you need to give more information – from the information you present it's not clear what type your data really is. It is possible that you have a data.frame, in which case you can go with Simon's answer. However, your output could easily be a vector with named elements, too:

dat <- c(PC1=6.403056e-04, PC2=5.433502e-04, PC3=7.888467e-05, PC4=6.776798e-05, PC5=2.589316e-05)
> dat
         PC1          PC2          PC3          PC4          PC5 
6.403056e-04 5.433502e-04 7.888467e-05 6.776798e-05 2.589316e-05 
> class(dat)
[1] "numeric"

In which case you already have what you are after, except for element labels. If you really needed to get rid of those you could drop them like this:

> names(dat)
[1] "PC1" "PC2" "PC3" "PC4" "PC5"
> names(dat) <- NULL
> dat
[1] 6.403056e-04 5.433502e-04 7.888467e-05 6.776798e-05 2.589316e-05

However, removing them probably has no real benefit.

Upvotes: 1

Simon
Simon

Reputation: 10841

If I understand your need correctly, you have a data frame dat like this:

> dat = data.frame(PC1=6.403056e-04, PC2=5.433502e-04, PC3=7.888467e-05, PC4=6.776798e-05,PC5=2.589316e-05) 
> dat
           PC1          PC2          PC3          PC4          PC5
1 0.0006403056 0.0005433502 7.888467e-05 6.776798e-05 2.589316e-05

If so, you can get a vector as follows:

> as.numeric(dat[1,])
[1] 6.403056e-04 5.433502e-04 7.888467e-05 6.776798e-05 2.589316e-05

... based on the answer to Convert a row of a data frame to vector.

Upvotes: 0

Related Questions