Displaying 5 results from an estimated 5 matches for "3_cat".
2018 May 11
3
add one variable to a data frame
...t;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 c...
2018 May 11
0
add one variable to a data frame
...gs 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...
2018 May 11
3
add one variable to a data frame
...gs 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...
2018 May 11
2
add one variable to a data frame
...the
price for this efficiency is that it depends completely on the data beig
grouped in order as the OP showed. It will fail if this is not the case.
If, for example, the data appeared as:
> set.seed(1234)
> ix <- sample(1:12)
> dat1[ix,]
N B
2 2 29_log
7 7 1_log
11 11 3_cat
6 6 1_log
10 10 1_log
5 5 27_cat
1 1 29_log
12 12 3_cat
3 3 29_log
8 8 1_log
4 4 27_cat
9 9 1_log
then Don's solution will still work. The above doesn't.
So this emphasizes the importance of precisely and completely specifying
the nature of your data. Hence: which is it...
2018 May 11
0
add one variable to a data frame
...er, even more basic:
tmp1 <- unique(dat1$B)
tmp2 <- seq_along(tmp1)
dat1$C <- tmp2[ match( dat1$B, tmp1) ]
> dat1
N B C
1 1 29_log 1
2 2 29_log 1
3 3 29_log 1
4 4 27_cat 2
5 5 27_cat 2
6 6 1_log 3
7 7 1_log 3
8 8 1_log 3
9 9 1_log 3
10 10 1_log 3
11 11 3_cat 4
12 12 3_cat 4
As a single line command:
dat1$C <- seq(length(unique(dat1$B)))[ match( dat1$B, unique(dat1$B) )]
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
?On 5/11/18, 12:04 PM, "R-help o...