Pulse
Pulse

Reputation: 897

Producing a new dataframe from an old dataframe?

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

Answers (2)

Andy Clifton
Andy Clifton

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

Jd Baba
Jd Baba

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

Related Questions