Skip to content

Selecting elements in lists with a row condition

7 messages · Francesca PANCOTTO, arun, Jim Lemon +1 more

#
Hi,
Try:

If `lst1` is the list:
do.call(rbind,lapply(lst1,function(x) x[x[,"p_made"]==406,]))
A.K.
On Tuesday, February 4, 2014 8:53 AM, Francesca <francesca.pancotto at gmail.com> wrote:
Dear Contributors
sorry but the message was sent involuntary.
I am asking some advice on how to solve the following problem.
I have a list composed of 78 elements, each of which is a matrix of factors
and numbers, similar to the following

bank_name?  date px_last_CIB? ? ?  Q.Y? ? p_made p_for
1? ? ?  CIB 10/02/06? ? ? ? 1.33 p406-q406? ? 406? ?  406
2? ? ?  CIB 10/23/06? ? ? ? 1.28 p406-q406? ? 406? ?  406
3? ? ?  CIB 11/22/06? ? ? ? 1.28 p406-q406? ? 406? ?  406
4? ? ?  CIB 10/02/06? ? ? ? 1.35 p406-q107? ? 406? ?  107
5? ? ?  CIB 10/23/06? ? ? ? 1.32 p406-q107? ? 406? ?  107
6? ? ?  CIB 11/22/06? ? ? ? 1.32 p406-q107? ? 406? ?  107


Each of these matrixes changes for the column name bank_name and for the
suffix _CIB which reports the name as in bank_name. Moreover each matrix as
a different number of rows, so that I cannot transform it into a large
matrix.

I need to create a matrix made of the rows of each element of the list that
respect the criterium
that the column p_made is = to 406.
I need to pick each of the elements of each matrix that is contained in the
list elements, that satisfy this condition.

It seems difficult to me but perhaps is super easy.
Thanks for any help you can provide.

Francesca
On 4 February 2014 12:42, Francesca <francesca.pancotto at gmail.com> wrote:

            

  
    
#
Hi,
Looks like the colnames of list elements are not the same.
For e.g.
lst1 <- list(structure(list(bankname = structure(c(1L, 1L, 1L, 1L, 1L, 
1L), .Label = "CIB", class = "factor"), date = structure(c(1L, 
2L, 3L, 1L, 2L, 3L), .Label = c("10/02/06", "10/23/06", "11/22/06"
), class = "factor"), px_last_CIB = c(1.33, 1.28, 1.28, 1.35, 
1.32, 1.32), Q.Y = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("p406-q107", 
"p406-q406"), class = "factor"), p_made = c(406L, 406L, 406L, 
406L, 406L, 406L), p_for = c(406L, 406L, 406L, 107L, 107L, 107L
)), .Names = c("bankname", "date", "px_last_CIB", "Q.Y", "p_made", 
"p_for"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6")), structure(list(bank_name = structure(c(1L, 1L, 
1L, 1L), .Label = "CBA", class = "factor"), date = structure(c(1L, 
2L, 3L, 1L), .Label = c("10/02/06", "10/23/06", "11/22/06"), class = "factor"), 
??? px_last_CIB = c(1.33, 1.28, 1.28, 1.35), Q.Y = structure(c(2L, 
??? 2L, 2L, 1L), .Label = c("p406-q107", "p406-q406"), class = "factor"), 
??? p_made = c(406L, 406L, 402L, 402L), p_for = c(406L, 406L, 
??? 402L, 107L)), .Names = c("bank_name", "date", "px_last_CIB", 
"Q.Y", "p_made", "p_for"), class = "data.frame", row.names = c("1", 
"2", "3", "4")), structure(list(bank_name = structure(c(1L, 1L, 
1L, 1L), .Label = "CAA", class = "factor"), date = structure(c(1L, 
2L, 3L, 1L), .Label = c("10/02/06", "10/23/06", "11/22/06"), class = "factor"), 
??? px_last_CIB = c(1.33, 1.28, 1.28, 1.35), Q.Y = structure(c(2L, 
??? 2L, 2L, 1L), .Label = c("p406-q107", "p406-q406"), class = "factor"), 
??? p_made = c(401L, 401L, 406L, 402L), p_for = c(401L, 401L, 
??? 406L, 107L)), .Names = c("bank_name", "date", "px_last_CIB", 
"Q.Y", "p_made", "p_for"), class = "data.frame", row.names = c("1", 
"2", "3", "4")))

names(lst1[[2]])[1]
#[1] "bank_name"


?names(lst1[[2]])[1]
#[1] "bank_name"

do.call(rbind,lapply(lst1,function(x) x[x[,"p_made"]==406,]))
Error in match.names(clabs, names(xi)) : 
? names do not match previous names


?lst2 <- lapply(lst1,function(x) {names(x) <-names(lst1[[2]]);x})
do.call(rbind,lapply(lst2,function(x) x[x[,"p_made"]==406,]))
?? bank_name???? date px_last_CIB?????? Q.Y p_made p_for
1??????? CIB 10/02/06??????? 1.33 p406-q406??? 406?? 406
2??????? CIB 10/23/06??????? 1.28 p406-q406??? 406?? 406
3??????? CIB 11/22/06??????? 1.28 p406-q406??? 406?? 406
4??????? CIB 10/02/06??????? 1.35 p406-q107??? 406?? 107
5??????? CIB 10/23/06??????? 1.32 p406-q107??? 406?? 107
6??????? CIB 11/22/06??????? 1.32 p406-q107??? 406?? 107
11?????? CBA 10/02/06??????? 1.33 p406-q406??? 406?? 406
21?????? CBA 10/23/06??????? 1.28 p406-q406??? 406?? 406
31?????? CAA 11/22/06??????? 1.28 p406-q406??? 406?? 406
A.K.
On Tuesday, February 4, 2014 3:01 PM, Francesca Pancotto <francesca.pancotto at gmail.com> 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?
f.


----------------------------------
Francesca Pancotto
Universit? degli Studi di Modena e Reggio Emilia
Palazzo Dossetti - Viale Allegri, 9 - 42121 Reggio?Emilia
Office: +39 0522 523264
Web: https://sites.google.com/site/francescapancotto/
----------------------------------

Il giorno 04/feb/2014, alle ore 16:42, arun <smartpink111 at yahoo.com> ha scritto:

Hi,
#
On 02/05/2014 06:54 AM, Francesca Pancotto wrote:
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
#
On Feb 4, 2014, at 11:54 AM, Francesca Pancotto wrote:

            
As teh message says, you are either  working with a set of list which have different names or you are creating data.frames with that problem. What does this show"

sapply(bank.list, names)

If they are all the same, then the output should be very , very regular.