[R] improvement
Sebastian Kruk
residuo.solow at gmail.com
Sun May 16 10:28:59 CEST 2010
Hi, if i just want a vector filled with names which has length(index) > 0.
For example if
nombreC <- c("Juan", "Carlos", "Ana", "María")
nombreL <- c("Juan Campo", "Carlos Gallardo", "Ana Iglesias", "María
Bacaldi", "Juan Grondona", "Dario Grandineti", "Jaime Acosta",
"Lourdes Serrano")
I would like to obtain a matrix called vaca with two column, name and
index, name is nombreC's element and index is the position in nombreL,
I don't want info about nombreC which no appear in nombreL. And I
would like to count how many cases appear.
For example vaca:
name index
1 Juan 1
2 Juan 5
3 Carlos 2
4 Ana 3
5 María 4
Code is it:
vaca <- do.call(rbind,lapply(noquote(nombreC),function(.name) {
index <- grep(.name,noquote(nombreL))
index <- if( length(index) > 0) index else 0
data.frame(name=.name,index=index)
}))
vaca <- vaca[vaca$index>0,]
cuenta <- nrow(vaca)
Thanks,
Sebastia´n
2010/5/11 <markleeds en verizon.net>:
> Hi: I added another column to make the output more understandable. I hope it
> helps.
>
>
> do.call(rbind,lapply(nombreC,function(.name) {
> index <- grep(.name,nombreL)
> nummatches <- if (length(index) > 0) length(index) else 0
> index <- if( length(index) > 0) index else 0
> data.frame(name=.name,index=index,nummatches=nummatches)
> }))
>
More information about the R-help
mailing list