Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a from, to network in three column data frame in r

Tags:

r

i hope everyone is doing well, I have problem to make a network of editors in a wikipedia articles within a single article (when one article finish start another article networks) the networks will be later used to find the degree centrality of both users and articles. the data frame looks like

faID           uName                     time_Stamp
 1               Qaless                 2003-09-06T20:27:00Z
 1               Austin                 2003-10-31T06:07:03Z
 1               SimonP                 2004-02-10T19:15:56Z
 1               SimonP                 2004-02-10T19:23:44Z
 1             Moncrief                2004-02-10T19:28:09Z
 1             Moncrief                2004-02-10T19:28:48Z
 1                  Rbs                2004-02-10T20:21:35Z
 1            Camembert                2004-02-10T20:27:34Z
 2             Moncrief                2004-02-10T20:29:33Z
 2                  Rbs                2004-02-10T20:39:33Z
 2              Jason M                2004-05-18T23:54:15Z
 2             Rickyrab                2004-05-28T05:35:32Z
 2             Rickyrab                2004-05-28T05:37:10Z
 2              Postdlf                2004-06-08T03:26:25Z
 2              Modster               2004-08-10T17:22:37Z
 3            PhilHibbs               2004-08-23T14:09:54Z
 3             Sfoskett               2004-09-10T18:22:15Z
 3               Dalton               2004-09-12T17:34:13Z
 3               Dalton               2004-09-12T17:38:35Z
 3      Ta bu shi da yu               2004-09-17T07:24:10Z

I want to have a data frame of network which will looks like

faid      to         from        time stamp 
 1         Qaless    Qaless        2003-09-06T20:27:00Z
 1        Qaless     Austin        2003-10-31T06:07:03Z
 1        Austin     SimonP        2004-02-10T19:15:56Z
 1        SimonP     SimonP        2004-02-10T19:23:44Z
 1        SimonP     Moncrief      2004-02-10T19:28:09Z
 1        Moncrief   Moncrief      2004-02-10T19:28:48Z
 1        Moncrief     Rbs         2004-02-10T20:21:35Z
 1        Camembert    Rbs         2004-02-10T20:27:34Z
 2        Moncrief   Moncrief      2004-02-10T20:29:33Z
 2        Moncrief    Rbs          2004-02-10T20:39:33Z
 2        Rbs        Jason M       2004-05-18T23:54:15Z
 2        jason M    Rickyrab     2004-05-28T05:35:32Z
 2        Rickyrab  Rickyrab      2004-05-28T05:37:10Z
 2        Rickyrab     Postdlf    2004-06-08T03:26:25Z
 2        Postdlf    modster      2004-08-10T17:22:37Z
 3        PhilHibbs PhilHibbs     2004-08-23T14:09:54Z
 3        PhilHibbs Sfoskett      2004-09-10T18:22:15Z 
 3        Sfoskett  Dalton        2004-09-12T17:34:13Z
 3        Dalton    Dalton        2004-09-12T17:38:35Z 
 3    dalton     Ta bu shi da yu  2004-09-17T07:24:10Z

general explanations are to--> the person edit( i.e the with edit before the next person(below row) from--> the person edit( below the person editing earlier) so any help for the solution.

like image 873
Naveed Khan Wazir Avatar asked May 20 '15 09:05

Naveed Khan Wazir


People also ask

How do I add columns from one Dataframe to another in R?

How do I add a column to a DataFrame in R? To add a new column to a dataframe in R you can use the $-operator. For example, to add the column “NewColumn”, you can do like this: dataf$NewColumn <- Values . Now, this will effectively add your new variable to your dataset.

How do you find the common values in three columns in R?

In R, you can use Reduce + intersect to get common values from all the columns.

How do I create a subset of a Dataframe in R?

Subset a Data Frame with Base R Extract[] To specify a logical expression for the rows parameter, use the standard R operators. If subsetting is done by only rows or only columns, then leave the other value blank. For example, to subset the d data frame only by rows, the general form reduces to d[rows,] .

How do I create a data frame structure in R?

The structure of the data frame can be seen by using str() function. 'data. frame': 5 obs.


2 Answers

Here's a possible data.table solution using the newest data.table version

library(data.table) # v 1.9.6+
setDT(df)[, to := shift(uName, fill = uName[1L]), by = faID]
setnames(df, "uName", "from")
df
#     faID            from           time_Stamp        to
#  1:    1          Qaless 2003-09-06T20:27:00Z    Qaless
#  2:    1          Austin 2003-10-31T06:07:03Z    Qaless
#  3:    1          SimonP 2004-02-10T19:15:56Z    Austin
#  4:    1          SimonP 2004-02-10T19:23:44Z    SimonP
#  5:    1        Moncrief 2004-02-10T19:28:09Z    SimonP
#  6:    1        Moncrief 2004-02-10T19:28:48Z  Moncrief
#  7:    1             Rbs 2004-02-10T20:21:35Z  Moncrief
#  8:    1       Camembert 2004-02-10T20:27:34Z       Rbs
#  9:    2        Moncrief 2004-02-10T20:29:33Z  Moncrief
# 10:    2             Rbs 2004-02-10T20:39:33Z  Moncrief
# 11:    2         Jason M 2004-05-18T23:54:15Z       Rbs
# 12:    2        Rickyrab 2004-05-28T05:35:32Z   Jason M
# 13:    2        Rickyrab 2004-05-28T05:37:10Z  Rickyrab
# 14:    2         Postdlf 2004-06-08T03:26:25Z  Rickyrab
# 15:    2         Modster 2004-08-10T17:22:37Z   Postdlf
# 16:    3       PhilHibbs 2004-08-23T14:09:54Z PhilHibbs
# 17:    3        Sfoskett 2004-09-10T18:22:15Z PhilHibbs
# 18:    3          Dalton 2004-09-12T17:34:13Z  Sfoskett
# 19:    3          Dalton 2004-09-12T17:38:35Z    Dalton
# 20:    3 Ta bu shi da yu 2004-09-17T07:24:10Z    Dalton
like image 137
David Arenburg Avatar answered Oct 31 '22 23:10

David Arenburg


If df is your original data.frame, you can do:

transform(df, 
             from = uName, 
             to = ave(as.character(uName), faID, FUN = function(x) c(x[1L], head(x,-1L))), 
             uName = NULL
           )

#    faID           time_Stamp            from        to
# 1     1 2003-09-06T20:27:00Z          Qaless    Qaless
# 2     1 2003-10-31T06:07:03Z          Austin    Qaless
# 3     1 2004-02-10T19:15:56Z          SimonP    Austin
# 4     1 2004-02-10T19:23:44Z          SimonP    SimonP
# 5     1 2004-02-10T19:28:09Z        Moncrief    SimonP
# 6     1 2004-02-10T19:28:48Z        Moncrief  Moncrief
# 7     1 2004-02-10T20:21:35Z             Rbs  Moncrief
# 8     1 2004-02-10T20:27:34Z       Camembert       Rbs
# 9     2 2004-02-10T20:29:33Z        Moncrief  Moncrief
# 10    2 2004-02-10T20:39:33Z             Rbs  Moncrief
# 11    2 2004-05-18T23:54:15Z         Jason M       Rbs
# 12    2 2004-05-28T05:35:32Z        Rickyrab   Jason M
# 13    2 2004-05-28T05:37:10Z        Rickyrab  Rickyrab
# 14    2 2004-06-08T03:26:25Z         Postdlf  Rickyrab
# 15    2 2004-08-10T17:22:37Z         Modster   Postdlf
# 16    3 2004-08-23T14:09:54Z       PhilHibbs PhilHibbs
# 17    3 2004-09-10T18:22:15Z        Sfoskett PhilHibbs
# 18    3 2004-09-12T17:34:13Z          Dalton  Sfoskett
# 19    3 2004-09-12T17:38:35Z          Dalton    Dalton
# 20    3 2004-09-17T07:24:10Z Ta bu shi da yu    Dalton
like image 41
Colonel Beauvel Avatar answered Oct 31 '22 23:10

Colonel Beauvel