dataframe - reshape data frame in R -


I have a data frame that I need to change, replacing duplicated values ​​in single columns in a row with multiple columns. is . I know this should be simple, but I do not understand how to do it, and I need to use which innovation / cast function is available.

The share of my data looks like this:

  In the source ID information 1 842701 1 2 out 842701 1 3 218465 9 2 2 4 Out 218465 9 2 2 22181760 3 6 In 39338740 4 7 Out 9428 5 I want to show it like this:  
  Information in ID 1 842701 1 1 2 2 21846591 1 1 2 3 22181760 1 3 4 39338740 1 0 4 5 9428 1 1 5  

And so on, preserving all the remaining columns (which are similar to a given entry).

I really appreciate some help TIA

Here's a way reshape2

  Library (reshape2) res & lt; - dcast (transform (df, index = 1, id = factor (id, level = unique (id)), id ~ source, value.var = "indx", fill = 0) res # ID out # 1 842701 1 1 # 2 21846591 1 1 # 3 22181760 1 0 # 4 39338740 1 0 # 5 9428 0 1  

or

  res1 and lt; - as. Data.frame.matrix (table (conversion (df, id = factor (id, level = unique (id)), [2: 1])  

update

  dcast (transform (df1, index = 1, id = factor (id, level = unique), ... ~ source, value var = "indx", fill = 0) # View ID Only in Out # 1 842701 1 1 1 # 2 21846591 2 1 1 # 3 22181760 3 1 0 # 4 39338740 4 1 0 # 5 9428 5 0 1  

You reshape < You can also use asepep (DF1, Indx = 1), ID + + C ("ID") and / code> to base R

  res2 < "," Information "), timewire =" source ", direction =" wide ") res2 [, 3: 4] [is.na (res2) [, 3: 4]] <- 0 res2 # id info indx .index.Out # 1 842701 1 1 1 # 3 21846591 2 1 1 # 5 22181760 3 1 0 # 6 39338740 4 1 0 # 7 9428 5 0 1  

data
  df & lt; - Structure (list (source = C ("in", "out", "in", "out", "in", "in", "out"), id = c (842701l, 842701l, 2184659 Name = C ("source", "id"), class = "data.frame", line.name = c ("1", "2", "L", "L", " , "3", "4", "5", "6", "7")) df1 & lt; - Structure (list = C ("in", "out", "in", "out", "in", "in", "out"), id = c (842701l, 842701l, 218465l, 21846591 L, 22181760 L, 39338740 L, 9428 L), Data = C (1L, 1L, 2L, 2L, 3L, 4L, 5L)). Name = C ("source", "id", "info"), class = "data.frame", line.Name = c ("1", "2", "3", "4", "5" , "6", "7"))  


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -