如何将新列添加到数据框(前端不结束)? [英] How to add new column to an dataframe (to the front not end)?

查看:111
本文介绍了如何将新列添加到数据框(前端不结束)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将新变量添加到现有的数据框架中,但是我想要添加到前端不能结束。
例如我的数据框是

How to add a new variable to an existing data frame, but I want to add to the front not end. eg. my dataframe is

b c d
1 2 3
1 2 3
1 2 3

我想添加一个新的变量a,所以数据框看起来像

I want to add a new variable a, so the dataframe will looks like

a b c d
0 1 2 3
0 1 2 3
0 1 2 3


推荐答案

使用 cbind eg

df <- data.frame(b = runif(6), c = rnorm(6))
cbind(a = 0, df)

给:

> cbind(a = 0, df)
  a         b          c
1 0 0.5437436 -0.1374967
2 0 0.5634469 -1.0777253
3 0 0.9018029 -0.8749269
4 0 0.1649184 -0.4720979
5 0 0.6992595  0.6219001
6 0 0.6907937 -1.7416569

这篇关于如何将新列添加到数据框(前端不结束)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆