social networking - Importing non-rectangular data as rectangular in R -


i need load social network data each user has unknown , potentially large number of friends, stored text file of following format:

userid: friendid1, friendid2, ... 1: 12, 33 2: 3: 4, 6, 10, 15, 16 

into two-column data.frame:

  userid friendid 1      1       12 2      1       33 3      3        4 4      3        6 5      3       10 6      3       15 7      3       16 

how in r?

reading, filling , reshaping inefficient requires keep in memory many columns full of na.

related questions here, , here.

if have colon delimiter, use read.table header = false data r, consider using csplit "splitstackshape" package.

mydf <- read.table("test.txt", sep = ":", header = false) mydf ##   v1                v2 ## 1  1            12, 33 ## 2  2                   ## 3  3  4, 6, 10, 15, 16  library(splitstackshape) csplit(mydf, "v2", ",", "long") ##    v1 v2 ## 1:  1 12 ## 2:  1 33 ## 3:  3  4 ## 4:  3  6 ## 5:  3 10 ## 6:  3 15 ## 7:  3 16 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -