1

I have a dataset that looks like this:

   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15  
1   1  1  1  1  1  1  1  1  1   1   1   1   1   1   1  
2   1  1  1  1  1  1  1 -1 -1  -1  -1  -1  -1  -1  -1  
3   1  1  1 -1 -1 -1 -1  1  1   1   1  -1  -1  -1  -1 
4   1  1  1 -1 -1 -1 -1 -1 -1  -1  -1   1   1   1   1  
5   1 -1 -1  1  1 -1 -1  1  1  -1  -1   1   1  -1  -1  
6   1 -1 -1  1  1 -1 -1 -1 -1   1   1  -1  -1   1   1  
7   1 -1 -1 -1 -1  1  1  1  1  -1  -1  -1  -1   1   1  
8   1 -1 -1 -1 -1  1  1 -1 -1   1   1   1   1  -1  -1  
9  -1  1 -1  1 -1  1 -1  1 -1   1  -1   1  -1   1  -1  
10 -1  1 -1  1 -1  1 -1 -1  1  -1   1  -1   1  -1   1  
11 -1  1 -1 -1  1 -1  1  1 -1   1  -1  -1   1  -1   1  
12 -1  1 -1 -1  1 -1  1 -1  1  -1   1   1  -1   1  -1  
13 -1 -1  1  1 -1 -1  1  1 -1  -1   1   1  -1  -1   1 
14 -1 -1  1  1 -1 -1  1 -1  1   1  -1  -1   1   1  -1  
15 -1 -1  1 -1  1  1 -1  1 -1  -1   1  -1   1   1  -1  
16 -1 -1  1 -1  1  1 -1 -1  1   1  -1   1  -1  -1   1  

and I want to combine the first three colums into a single base on some principle:

 1  1  1 → 1  
 1 -1 -1 → 2  
-1  1 -1 → 3  
-1 -1  1 → 4 

This is my first time to use R language. I have no idea about how to do it. Does anyone have some simple pieces of code to do this? Thanks in advance!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Stacy
  • 83
  • 6
  • Are just those 4 the conditions you would apply? – Michele Apr 19 '13 at 07:33
  • Have you any criteria to apply? Try rowSums(matrix(1:12, ncol = 3)) – Paulo E. Cardoso Apr 19 '13 at 07:39
  • See also this question how to create a lookup table: http://stackoverflow.com/questions/16092503/dictionaries-and-pairs-in-r/16092717. You could paste your three columns together to create a unqiue identifier, and create a lookup table with the translation from that key to the associated value. See `paste` for pasting together strings/numbers. – Paul Hiemstra Apr 19 '13 at 07:40
  • In this case, I only have 4 conditions to apply. In other example, there exists 8 possible combinations of the first three columns. – Stacy Apr 19 '13 at 09:21
  • Step by step 1. How do I convert any 3 columns into a single one? 2. Convert 3 columns into a singlle one, and how I need sets of 2 columns, where these 2 columns are waht I got from the step 1. How do I obtain the sets? (Which is 2 combination of 2 new columns with each of then derived from three into 1) – Stacy Apr 21 '13 at 04:20

2 Answers2

4

I pretend your data frame is called df...

test <- apply(df[1:3], 1, paste, collapse="") # this will merge the numbers of the first 3
                                         # for each row

result <- sapply(test, switch, '111' = 1, '1-11' = 2, '-11-1' = 3, '-1-11' = 4)

in case result is a list use unlist

Michele
  • 8,563
  • 6
  • 45
  • 72
  • Hello, Michele I really liked your solution. I have a small question here. I checked the lenght of "result" is 16 but when I use `unlist(result)` it will be changed to a "numeric" vector of length 12. Did I do anything wrong here ? – Jd Baba Apr 19 '13 at 14:08
  • @Jdbaba Probably because `length(result) = nrow(df)` after using `sapply`. However, there could be some `NULL` values in the list object `result` that are ignored by `unlist` which returns, in a numeric vector, only the non null values. This happens if the options you provide `swicth` with don't cover all the possible unique value among `df`. If you know them in advance just add to the four I've used, otherwise use `unique(test)` to have them all. – Michele Apr 19 '13 at 14:25
2

Given that there's 8 possible combinations of values from the first three columns, you might need to be a bit more specific on how you want to code your combined result. That said, this will give a mapping from those values to a single number. Assume your dataset is a data frame called dat:

as.numeric(factor(do.call(paste, dat[1:3])))
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • I totally ignored the fact you mention, thanks for your remind! I tried this instruction code and got this result: [1] 4 4 4 4 3 3 3 3 2 2 2 2 1 1 1 1 I just wondered if there is any way to change the result to : [1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 – Stacy Apr 19 '13 at 09:10
  • 1
    @Stacy just reverse code using `5 -` as in `5 - as.numeric(factor(do.call(paste, dat[1:3])))` – Tyler Rinker Apr 19 '13 at 13:29
  • 1
    @Stacy try something like `levels(result) <- c(1,2,3,4)`, where `result` is the result from @Hong code without `as.numeric`. – Michele Apr 19 '13 at 14:29
  • All of your suggestions are helpful. I tried all of them. Step by step, I got further, deeper understanding. Thanks for all of you. :) – Stacy Apr 20 '13 at 15:52
  • Step by step 1. How do I convert any 3 columns into a single one? 2. Convert 3 columns into a singlle one, and how I need sets of 2 columns, where these 2 columns are waht I got from the step 1. How do I obtain the sets? (Which is 2 combination of 2 new columns with each of then derived from three into 1) – Stacy Apr 21 '13 at 04:06