[R] add one variable to a data frame

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Fri May 11 20:39:34 CEST 2018


Hi,

Here's one way to approach it, using the coercion of factor to numeric.

Note that I changed your data.frame() statement to avoid coercing
strings to factors, just to make it simpler to set the levels.

dat1 <-data.frame(N=seq(1, 12,1), B=c("29_log","29_log", "29_log",
"27_cat", "27_cat", "1_log", "1_log", "1_log", "1_log", "1_log",
"3_cat", "3_cat"), stringsAsFactors=FALSE)


dat1$C1 <- as.numeric(factor(dat1$B, levels=unique(dat1$B)))

And here's a way using rle()

dat1$C2 <- rep(seq_len(length(unique(dat1$B))),
times=rle(as.vector(dat1$B))$lengths)

(That second will work even if B is a factor.)

> dat1
    N      B C1 C2
1   1 29_log  1  1
2   2 29_log  1  1
3   3 29_log  1  1
4   4 27_cat  2  2
5   5 27_cat  2  2
6   6  1_log  3  3
7   7  1_log  3  3
8   8  1_log  3  3
9   9  1_log  3  3
10 10  1_log  3  3
11 11  3_cat  4  4
12 12  3_cat  4  4


Sarah

On Fri, May 11, 2018 at 1:52 PM, Ding, Yuan Chun <ycding using coh.org> wrote:
> Hi All,
>
> I have a data frame dat1:
> dat1 <-data.frame(N=seq(1, 12,1), B=c("29_log","29_log", "29_log", "27_cat", "27_cat",
>                                                                        "1_log", "1_log", "1_log", "1_log", "1_log",
>                                                                         "3_cat", "3_cat"))
>
> Then I need to add one column or variable to reflect uniqueness of B variable in sequential order as below.
> dat1$C <-c(1,1,1,2,2,3,3,3,3,3,4,4)
>
> I only show 12 rows, my real data frame has over 1000 rows, I can not manually to add column C.
>
> It should be easy, but I can not figure out. Can you help me?
>
> Thanks,
>
> Ding
>
-- 
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list