add one variable to a data frame

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

add one variable to a data frame

Ding, Yuan Chun
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


---------------------------------------------------------------------
-SECURITY/CONFIDENTIALITY WARNING-
This message (and any attachments) are intended solely f...{{dropped:22}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

Sarah Goslee
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 <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

Ding, Yuan Chun
Hi Sarah,

Thank you so much!! I got your good ideas.

Ding

-----Original Message-----
From: Sarah Goslee [mailto:[hidden email]]
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 <[hidden email]> 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)
---------------------------------------------------------------------

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

MacQueen, Don
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" <[hidden email] on behalf of [hidden email]> wrote:

    Hi Sarah,
   
    Thank you so much!! I got your good ideas.
   
    Ding
   
    -----Original Message-----
    From: Sarah Goslee [mailto:[hidden email]]
    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 <[hidden email]> 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)
    ---------------------------------------------------------------------
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

Bert Gunter-2
In reply to this post by Sarah Goslee
Sarah et. al.:

As a matter of aesthetics (i.e. my personal ocd-ness) I prefer using the
public API of an object, i.e. *not* to makes use of the representation of a
factor as essentially an integer vector with labels, but rather to use its
documented behavior. (Feel free to ignore this remark!)

Anyway,

>cumsum(!duplicated(dat1$B))
 [1] 1 1 1 2 2 3 3 3 3 3 4 4

will do it.

This is very efficient (almost certainly of no concern here, btw). But 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? -- all the groups appearing
together or possibly mixed up?

But I have another question: why do this at all? The new column adds no new
information -- I believe that anything you want to do with the integer
codes can be done in R with the original factor representation (and just as
efficiently, as Sarah's "aesthetically displeasing to Bert" suggestion
makes clear). Note: counterexample welcome! So as AFAICS, there is no need
for this at all.

Cheers,
Bert





Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, May 11, 2018 at 11:39 AM, Sarah Goslee <[hidden email]>
wrote:

> 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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

Ding, Yuan Chun
In reply to this post by MacQueen, Don
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:[hidden email]]
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" <[hidden email] on behalf of [hidden email]> wrote:

    Hi Sarah,
   
    Thank you so much!! I got your good ideas.
   
    Ding
   
    -----Original Message-----
    From: Sarah Goslee [mailto:[hidden email]]
    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 <[hidden email]> 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)
    ---------------------------------------------------------------------
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

Ding, Yuan Chun
In reply to this post by Bert Gunter-2
Hi Bert,

Thank you very much for your beautiful input!!

I think I understand your concern regarding creating a unnecessary column.     Each duplicated rows in order represents a chromosome region containing two to ten adjacent loci.  When we present data, we want to show region1, region2, …;  I will make scatter plots for each region by selection of top loci based on  p value and effect size.  So I like to define a simple region tag or label using integer.  Anyway, there are a lot of more columns in the data frame.

Thanks again for this useful r-help platform.  I posted my question and then got answer after coming back from lunch.  I am a beginner in R, so question might be trivial.

Ding

From: Bert Gunter [mailto:[hidden email]]
Sent: Friday, May 11, 2018 1:37 PM
To: Sarah Goslee
Cc: Ding, Yuan Chun; 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.]
________________________________

Sarah et. al.:
As a matter of aesthetics (i.e. my personal ocd-ness) I prefer using the public API of an object, i.e. *not* to makes use of the representation of a factor as essentially an integer vector with labels, but rather to use its documented behavior. (Feel free to ignore this remark!)
Anyway,

>cumsum(!duplicated(dat1$B))
 [1] 1 1 1 2 2 3 3 3 3 3 4 4

will do it.
This is very efficient (almost certainly of no concern here, btw). But 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? -- all the groups appearing together or possibly mixed up?
But I have another question: why do this at all? The new column adds no new information -- I believe that anything you want to do with the integer codes can be done in R with the original factor representation (and just as efficiently, as Sarah's "aesthetically displeasing to Bert" suggestion makes clear). Note: counterexample welcome! So as AFAICS, there is no need for this at all.

Cheers,
Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, May 11, 2018 at 11:39 AM, Sarah Goslee <[hidden email]<mailto:[hidden email]>> wrote:
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 <[hidden email]<mailto:[hidden email]>> 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

______________________________________________
[hidden email]<mailto:[hidden email]> 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.



---------------------------------------------------------------------
-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 receive 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)
---------------------------------------------------------------------


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

MacQueen, Don
In reply to this post by Bert Gunter-2
Interesting comments, and they serve as a reminder that so much depends on:
  -- what is known or can be assumed about the structure of the incoming data
  -- what will be done with the data next
(and is this a one-time effort, or will it be repeated multiple times with similarly-structured but different incoming data?)

Indeed, my solution still works if the incoming data is in a different order, including a random order, but it doesn't necessarily assign the same integers to the same groups. Is that important? If it is, then a more complex strategy is required.

By way of a counterexample:

I sometimes find it useful to index groups by an integer value, particularly when the grouping is defined by the unique combinations values in two or more character variables. Suppose I'm doing an analysis for every subgroup. Then suppose I'm reviewing its results for a particular subgroup, and need to inspect the data for that subgroup. If I've created my index, and have it associated with the analysis results, then it's easier to use that index to extract the subset of data than it would be without it.

Of course, in this example I'm not analyzing on the grouping variables themselves, so it isn't quite a counterexample to Bert's suggestion that creating the "C" variable isn't need in the OP's example. But I think it shows some value to having the tools to create an integer index for each unique group in a data set, which is essentially what was asked for (in my view).

-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, 1:36 PM, "R-help on behalf of Bert Gunter" <[hidden email] on behalf of [hidden email]> wrote:

    Sarah et. al.:
   
    As a matter of aesthetics (i.e. my personal ocd-ness) I prefer using the
    public API of an object, i.e. *not* to makes use of the representation of a
    factor as essentially an integer vector with labels, but rather to use its
    documented behavior. (Feel free to ignore this remark!)
   
    Anyway,
   
    >cumsum(!duplicated(dat1$B))
     [1] 1 1 1 2 2 3 3 3 3 3 4 4
   
    will do it.
   
    This is very efficient (almost certainly of no concern here, btw). But 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? -- all the groups appearing
    together or possibly mixed up?
   
    But I have another question: why do this at all? The new column adds no new
    information -- I believe that anything you want to do with the integer
    codes can be done in R with the original factor representation (and just as
    efficiently, as Sarah's "aesthetically displeasing to Bert" suggestion
    makes clear). Note: counterexample welcome! So as AFAICS, there is no need
    for this at all.
   
    Cheers,
    Bert
   
   
   
   
   
    Bert Gunter
   
    "The trouble with having an open mind is that people keep coming along and
    sticking things into it."
    -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
   
    On Fri, May 11, 2018 at 11:39 AM, Sarah Goslee <[hidden email]>
    wrote:
   
    > 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 <[hidden email]> 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
    >
    > ______________________________________________
    > [hidden email] 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.
    >
   
    [[alternative HTML version deleted]]
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

Peter Dalgaard-2
In reply to this post by MacQueen, Don
Um, maybe just

dat1$C <- match(dat1$B, unique(dat1$B))

Indexing 1:k with numbers between 1 and k is a bit of a no-op...

AFAICT, this even works without stringsAsFactors=FALSE

-pd

> On 11 May 2018, at 21:30 , MacQueen, Don <[hidden email]> wrote:
>
> dat1$C <- seq(length(unique(dat1$B)))[ match( dat1$B, unique(dat1$B) )]

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: add one variable to a data frame

MacQueen, Don
Of course...
Thanks
-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, 3:50 PM, "R-help on behalf of peter dalgaard" <[hidden email] on behalf of [hidden email]> wrote:

    Um, maybe just
   
    dat1$C <- match(dat1$B, unique(dat1$B))
   
    Indexing 1:k with numbers between 1 and k is a bit of a no-op...
   
    AFAICT, this even works without stringsAsFactors=FALSE
   
    -pd
   
    > On 11 May 2018, at 21:30 , MacQueen, Don <[hidden email]> wrote:
    >
    > dat1$C <- seq(length(unique(dat1$B)))[ match( dat1$B, unique(dat1$B) )]
   
    --
    Peter Dalgaard, Professor,
    Center for Statistics, Copenhagen Business School
    Solbjerg Plads 3, 2000 Frederiksberg, Denmark
    Phone: (+45)38153501
    Office: A 4.23
    Email: [hidden email]  Priv: [hidden email]
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.