[R] Combinations of values in two columns
szehnder at uni-bonn.de
Fri Nov 1 11:07:59 CET 2013
You could use the data.table package
DT <- data.table(Friend1 = sample(LETTERS, 10, replace = TRUE), Friend2 = sample(LETTERS, 10, replace = TRUE), Indicator = 1)
ALL <- data.table(unique(expand.grid(DT)))
OTHERS <- ALL[!DT]
OTHERS[, Indicator := 0]
RESULT <- rbind(DT, ALL)
On 01 Nov 2013, at 10:32, Thomas <Thomas.Chesney at nottingham.ac.uk> wrote:
> I have data that looks like this:
> Friend1, Friend2
> A, B
> A, C
> B, A
> C, D
> And I'd like to generate some more rows and another column. In the new column I'd like to add a 1 beside all the existing rows. That bit's easy enough.
> Then I'd like to add rows for all the possible directed combinations of rows not included in the existing data. So for the above I think that would be:
> A, D
> D, A
> B, C
> C, B
> B, D
> C, A
> D, B
> D, C
> and then put a 0 in the column beside these.
> Can anyone suggest how to do this?
> I'm using R version 2.15.3.
> Thank you,
> Thomas Chesney
> This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.
> R-help at r-project.org mailing list
> 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