Reputation: 9875
I need to do many conversions at once ina certain file I'm running. I do not understand why converting something twice results in a different output.
given
x = c(140,190,190,185,195,195,195,130,150,125,50,30,15,45,55)
y = c(150,195,190,190,190,200,195,140,30,20,125,25,45,65,70)
This,
locs <- data.frame(x=x,y=y)
...works as expected, with two columns named x and y. However, if I do the following
locs <- as.data.frame(x=x,y=y)
it gives just one column named x, but with the values of y. Why is this? It is important for the larger files I'm running - I may have to change my whole approach.
Upvotes: 0
Views: 69
Reputation: 193677
data.frame
starts of with ...
, which is often populated with "tag/value" pairs to be combined as columns. Thus, data.frame(x = x, y = y)
would work as you expected.
as.data.frame
expects a single object that can be coerced into a data.frame
using one of the as.data.frame.*
methods. It also has a ...
argument, but this is used to pass arguments to the different as.data.frame
methods.
In other words, if you had a matrix
(m <- cbind(x, y)
), you could use as.data.frame(m)
and get the results you expected, though if you are starting with vectors, you might as well just do data.frame(x, y)
.
Upvotes: 1