[R] improvement

Sebastian Kruk residuo.solow at gmail.com
Mon May 17 15:33:03 CEST 2010


For example if nombreC <- nombreC <- c("Juan", "Carlos", "Ana", "María","Mario")

I do not want as a result:

    name index
1   Juan     1
2   Juan     5
3 Carlos     2
4    Ana     3
5  María     4
6  Mario     0

I want:

    name index
1   Juan     1
2   Juan     5
3 Carlos     2
4    Ana     3
5  María     4

As an result of the do.call I want a data frame where every name has
an index > 0.

2010/5/16  <markleeds en verizon.net>:
> Hi Sebastian: I'm not sure that I understand what you want but maybe below
> is what you want ? I just took out the nummatches
> column.
>
>
> 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")
>
> 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)
>          data.frame(name=.name,index=index)
> }))



More information about the R-help mailing list