[R] Selecting elements in lists with a row condition
Jim Lemon
jim at bitwrit.com.au
Tue Feb 4 22:48:21 CET 2014
On 02/05/2014 06:54 AM, Francesca Pancotto wrote:
> Hello A. k.
> thanks for the suggestion.
>
> I tried this but it does not work. I probably use it in the wrong way.
> This is what it tells me,
>
>
> do.call(rbind,lapply(bank.list,function(x) x[x[,"p_made"]==406,]))
>
> Errore in match.names(clabs, names(xi)) :
> names do not match previous names
>
> What am I doing wrong?
Hi Francesca,
This is not as elegant as Arun's solution, but it seems to work:
bank.list<-list()
bank.list[[1]]<-read.table(
text="bank_name date px_last_CIB Q.Y p_made p_for
CIB 10/02/06 1.33 p406-q406 406 406
CIB 10/23/06 1.28 p406-q406 406 406
CIB 11/22/06 1.28 p406-q406 406 406
CIB 10/02/06 1.35 p406-q107 406 107
CIB 10/23/06 1.32 p406-q107 406 107
CIB 11/22/06 1.32 p406-q107 406 107",header=TRUE)
bank.list[[2]]<-read.table(
text="bank_name date px_last_CIB Q.Y p_made p_for
DIB 10/02/06 1.33 p406-q406 406 406
DIB 10/23/06 1.28 p406-q406 406 406
DIB 11/22/06 1.28 p406-q406 406 406
DIB 10/02/06 1.35 p406-q107 406 107
DIB 10/23/06 1.32 p406-q107 406 107
DIB 11/22/06 1.32 p406-q107 406 107",header=TRUE)
bank.list[[3]]<-read.table(
text="bank_name date px_last_CIB Q.Y p_made p_for
EIB 10/02/06 1.33 p406-q406 406 406
EIB 10/23/06 1.28 p406-q406 406 406
EIB 11/22/06 1.28 p406-q406 406 406
EIB 10/02/06 1.35 p406-q107 406 107
EIB 10/23/06 1.32 p406-q107 406 107
EIB 11/22/06 1.32 p406-q107 406 107",header=TRUE)
get_rows<-function(x,field_name,field_value) {
return(x[x[,field_name]==field_value,])
}
collapse_df_list<-function(x,collapse_field) {
listlen<-length(x)
newdf<-x[[1]]
for(listel in 2:listlen) newdf<-rbind(newdf,x[[listel]])
return(newdf)
}
select_list<-lapply(bank.list,get_rows,"p_for",406)
merged_list<-collapse_df_list(select_list,"p_for")
Jim
More information about the R-help
mailing list