how to add a new column in R with a defined name

15 views
Skip to first unread message

lfa...@ucdavis.edu

unread,
Sep 25, 2016, 5:53:32 PM9/25/16
to Davis R Users' Group
how to add a new column in R with a defined name 

Brandon Hurr

unread,
Sep 25, 2016, 6:13:35 PM9/25/16
to Davis R Users' Group
What have you tried?

What do you want to put in the new column?

B
On Sun, Sep 25, 2016 at 14:53 <lfa...@ucdavis.edu> wrote:
how to add a new column in R with a defined name 

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Michael Hannon

unread,
Sep 25, 2016, 6:14:04 PM9/25/16
to davi...@googlegroups.com
You don't say exactly what you're adding the column to, but see below
for my guess:

> x <- data.frame(even=c(2, 4, 6), odd=c(1, 3, 5))
> x
even odd
1 2 1
2 4 3
3 6 5
> x <- data.frame(even=c(2, 4, 6), odd=c(1, 3, 5))
> x
even odd
1 2 1
2 4 3
3 6 5
> x$new <- c(4, 16, 25)
> x
even odd new
1 2 1 4
2 4 3 16
3 6 5 25


On Sun, Sep 25, 2016 at 2:53 PM, <lfa...@ucdavis.edu> wrote:
> how to add a new column in R with a defined name
>

Laila Fayyaz

unread,
Sep 25, 2016, 6:20:38 PM9/25/16
to davi...@googlegroups.com
Hi, 

I want to make a new column in trees and I want  to rename it as BD. I succeeded to make a new column as V4 but  I want to rename it as DB. I have attached a screen shot as well. 

thanks   

On Sun, Sep 25, 2016 at 3:13 PM, Brandon Hurr <brando...@gmail.com> wrote:
What have you tried?

What do you want to put in the new column?

B
On Sun, Sep 25, 2016 at 14:53 <lfa...@ucdavis.edu> wrote:
how to add a new column in R with a defined name 

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+unsubscribe@googlegroups.com.

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/UsP6iNFhqrU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+unsubscribe@googlegroups.com.



--
Laila Fayyaz
Viticulture and Enology- Walker lab
University of California

Davis, CA, 95616

2128 RMI North
595 Hilgard Lane



Screen Shot 2016-09-25 at 3.16.47 PM.png

Katherine Ransom

unread,
Sep 25, 2016, 6:29:06 PM9/25/16
to davis-rug

Just do trees$BD <- trees$Girth* pi to create the column with the correct name ( it will automatically be located last). This prevents having to rename. To rename the column you can also do names(trees)[4] <-  "BD"

Brandon Hurr

unread,
Sep 25, 2016, 6:35:00 PM9/25/16
to davi...@googlegroups.com
Katherine's right. It's two steps if you don't do it right the first time. 

Alternatively, in the tidyverse:

library(tidyverse) # install.packages("tidyverse") if you don't have it

trees %>%
mutate(BD = Girth * pi)

HTH,
B

Laila Fayyaz

unread,
Sep 25, 2016, 6:43:32 PM9/25/16
to davi...@googlegroups.com
Thank- you 

It works pretty well. 

 

On Sun, Sep 25, 2016 at 3:29 PM, Katherine Ransom <lockhart....@gmail.com> wrote:

Laila Fayyaz

unread,
Sep 25, 2016, 6:44:13 PM9/25/16
to davi...@googlegroups.com
Thank-you 
Reply all
Reply to author
Forward
0 new messages