简体   繁体   English

从旧数据框生成新的数据框?

[英]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 我想从一个old大数据cbind.data.frame (许多变量)生成一个new数据cbind.data.frame我使用cbind.data.frame函数,它就像这样

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$ ? 为什么var1仍然属于old$
I wanted to use just new$var1 but it returns object not found . 我想只使用new$var1但它返回object not found

What am I doing wrong? 我究竟做错了什么?

Combine both of the other other answers by doing this: 通过这样做结合其他两个答案:

New <- data.frame("var1" = old$var1, 
                  "var2" = old$var2, 
                  "var3" = old$var3) 

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. 现在,您将能够使用new$var1 ,依此类推。

Hope this solves your problem. 希望这能解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM