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
.
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
Post a Comment