[R] Selecting elements in lists with a row condition
arun
smartpink111 at yahoo.com
Tue Feb 4 22:32:37 CET 2014
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,
>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:
>
>
>Dear Contributors
>>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
>>
>>
>>--
>>
>>Francesca
>>
>>----------------------------------
>>Francesca Pancotto, PhD
>>Università di Modena e Reggio Emilia
>>Viale A. Allegri, 9
>>40121 Reggio Emilia
>>Office: +39 0522 523264
>>Web: https://sites.google.com/site/francescapancotto/
>>
>
>----------------------------------
>>
>>
>
>
>--
>
>Francesca
>
>----------------------------------
>Francesca Pancotto, PhD
>Università di Modena e Reggio Emilia
>Viale A. Allegri, 9
>40121 Reggio Emilia
>Office: +39 0522 523264
>Web: https://sites.google.com/site/francescapancotto/
>----------------------------------
>
> [[alternative HTML version deleted]]
>
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list