Reputation: 897
I want to produce a new
dataframe from an old
big one (many variables)
I use the cbind.data.frame
function and it goes like this
new <- cbind.data.frame(old$var1, old$var2, old$var3)
str(new)
'data.frame': 100 obs. of 3 variables:
$ old$var1 : num
Why does the var1 still belong to old$
?
I wanted to use just new$var1
but it returns object not found
.
What am I doing wrong?
Upvotes: 8
Views: 39944
Reputation: 5056
Combine both of the other other answers by doing this:
New <- data.frame("var1" = old$var1,
"var2" = old$var2,
"var3" = old$var3)
Upvotes: 20
Reputation: 6118
You are doing nothing wrong, you just need to rename the columns in your new data frame using:
names(new) <- c("var1","var2","var3")
Now, you will be able to use new$var1
, and so on.
Hope this solves your problem.
Upvotes: 3