[R] add one variable to a data frame
m@cqueen1 @end|ng |rom ||n|@gov
Sat May 12 00:27:25 CEST 2018
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).
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
Lab cell 925-724-7509
On 5/11/18, 1:36 PM, "R-help on behalf of Bert Gunter" <r-help-bounces using r-project.org on behalf of bgunter.4567 using gmail.com> 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!)
 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:
> ix <- sample(1:12)
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.
"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 <sarah.goslee using gmail.com>
> 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))),
> (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
> 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
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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