[R] R extract parts
Rui Barradas
rui1174 at sapo.pt
Wed Mar 28 01:36:24 CEST 2012
Hello,
>
> my idea is to get results like this:
> user, sector, source, destine, count, average
> 7 1 22 22 4 186.25 #
> (109+100+214+322)
> 7 2 161 97 1 68
> 7 2 97 97 1 196
> 7 2 97 22 1 427
> 7 2 22 22 2 383
>
Your second column, 'sector', comes from where? What is it?
Without it, try this.
text="
user pos communications source v_destine
7 1 109 22 22
7 2 100 22 22
7 3 214 22 22
7 4 322 22 22
7 5 69920 22 161
7 6 68 161 97
7 7 196 97 97
7 8 427 97 22
7 9 460 22 22
7 10 307 22 22
7 11 9582 22 22
7 12 55428 22 22
7 13 9192 22 22
7 14 19 22 22
"
df1 <- read.table(textConnection(text), header=TRUE)
inx <- df1$comm > 1000
comm1000 <- cumsum(inx)
result <- split(df1[!inx, ], list(comm1000[!inx], df1$source[!inx],
df1$v_destine[!inx]))
result <- sapply(result, function(x) c(x$user[1], x$source[1],
x$v_destine[1], nrow(x), mean(x$comm)))
result <- na.exclude(t(result))
rownames(result) <- 1:nrow(result)
colnames(result) <- c("user", "source", "v_destine", "count", "average")
attr(result, "na.action") <- NULL
attr(result, "class") <- NULL
result
Hope this helps,
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/R-extract-parts-tp4509042p4510566.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list