Reputation: 38
I have a data.frame that has a column which its attributes look like this:
$ variable : Named num 0.887 0.886 0.887 0.887 0.887 ...
..- attr(*, "names")= chr "2010-01-04 00:00" "2010-01-04 00:01" "2010-01-04"
And I'm trying to reply this format in another data.frame, but I can't make the variable a Named num, I tried this:
vec = rnorm(10)
names(vec) = letters[1:10]
dd = data.frame(a = vec)
str(dd)
And also this:
dd = data.frame(a = rnorm(10))
dd$a = setNames(dd$a, letters[1:10])
str(dd)
Which both return:
'data.frame': 10 obs. of 1 variable:
$ a: num 1.623 -0.178 0.988 -0.406 -0.554 ...
Does anybody know how can I convert the column into a Named num?
Thank you
Upvotes: 0
Views: 75
Reputation: 76402
Here is what I could come up with.
First create the vector, then dput
it to a structure
instruction creating the dataframe.
set.seed(1234)
vec <- rnorm(10)
names(vec) <- letters[1:10]
n <- length(vec)
dd <- structure(list(a = dput(vec)),
class = "data.frame", row.names = c(NA, n))
str(dd)
#'data.frame': 10 obs. of 1 variable:
# $ a: Named num -1.207 0.277 1.084 -2.346 0.429 ...
# ..- attr(*, "names")= chr "a" "b" "c" "d" ...
Upvotes: 1