[R] add one variable to a data frame

Ding, Yuan Chun ycding @ending from coh@org
Fri May 11 22:59:14 CEST 2018


Hi Don and Sarah,

Thanks a lot, I learned quite a few functions today.

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 = F)
dat1$C <-c(1,1,1,2,2,3,3,3,3,3,4,4)
#The match() function returns a vector of the position of first occurrence of the
#vector1 in vector2. If the element of the vector1 does not exist in vector2, 
#NA is returned.
dat1$don <- match(dat1$B, unique(dat1$B))

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

# rle, Compute the lengths and values of runs of equal values in a vector
# return a list with two elements, length of each value, and unique values
dat1$sarah2 <- rep(seq_len(length(unique(dat1$B))),
                   times=rle(as.vector(dat1$B))$lengths)

#seq_along(x) is the same as seq_len(length(x))
dat1$D <- rep(seq_along(unique(dat1$B)),
                   times=rle(as.vector(dat1$B))$lengths)

-----Original Message-----
From: MacQueen, Don [mailto:macqueen1 at llnl.gov] 
Sent: Friday, May 11, 2018 12:30 PM
To: Ding, Yuan Chun; Sarah Goslee
Cc: r-help mailing list
Subject: Re: [R] add one variable to a data frame

Sarah's solutions are good, and here's another, 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 on behalf of Ding, Yuan Chun" <r-help-bounces at r-project.org on behalf of ycding at coh.org> wrote:

    Hi Sarah,
    
    Thank you so much!! I got your good ideas.
    
    Ding
    
    -----Original Message-----
    From: Sarah Goslee [mailto:sarah.goslee at gmail.com] 
    Sent: Friday, May 11, 2018 11:40 AM
    To: Ding, Yuan Chun
    Cc: r-help mailing list
    Subject: Re: [R] add one variable to a data frame
    
    [Attention: This email came from an external source. Do not open attachments or click on links from unknown senders or unexpected emails.]
    
    
    
    
    
    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 at 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
    
    
    ---------------------------------------------------------------------
    -SECURITY/CONFIDENTIALITY WARNING-
    This message (and any attachments) are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to r
     eceive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)
    ---------------------------------------------------------------------
    
    ______________________________________________
    R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.
    



More information about the R-help mailing list