[R] extracting columns from a list
David Winsemius
dwinsemius at comcast.net
Mon May 26 21:30:14 CEST 2008
Vincent Goulet <vincent.goulet at act.ulaval.ca> wrote in
news:B99F4ABB-08AC-4470-A21C-D719D67C8D74 at act.ulaval.ca:
> Le ven. 23 mai à 09:37, mohamed nur anisah a écrit :
>
>> Dear all,
>>
>> i have 2 lists of data with each of the list contain 14 columns.
>
> No, you have one list with two elements; each is a 14-column data
> frame.
>
>> How am i going to extract column 12 and 13 from each of the list ??
>
> Let's call your list 'x'. Then
>
> lapply(x, "[", c(12, 13))
>
> or, better yet,
>
> lapply(x, "[", c("breakp.start", "breakp.end"))
>
> if I counted your columns correctly.
>
>> and can i combine my extracted columns to form a single list.
>
> You mean a single data frame? Given that the number of rows is not
> the same in both elements, no. If they were, using sapply() instead
> of lapply() would do the trick.
>
> HTH Vincent
(Many thanks, Dr Goulet, for your actuarial package.)
I am wondering if he meant to combine his 13 rows and append 22 rows with
a result of 35 rows and 2 columns. If so, then perhaps he should see:
?rbind
When I employ rbind to the components of your suggested object:
k0 <- lapply(x, "[", c("breakp.start", "breakp.end"))[[1]]
k1 <- lapply(x, "[", c("breakp.start", "breakp.end"))[[2]]
df2 <- rbind(k0,k1)
I get rather odd rownames (row 14 is named "110" followed by "23, "31",
etc) , but this fix "works" for me:
str(df2)
# 'data.frame':
rownames(df2) <- 1:35
> df2
breakp.start breakp.end
1 6978022 6992734
2 10249966 10277430
3 10955201 11008526
4 11045352 11316865
5 11814604 11937228
6 13847633 14515091
7 16022770 16199850
8 23625856 23754554
9 29676192 29763918
10 30164446 30611872
11 34558312 34734404
12 44009616 44088256
13 53582632 53654796
14 7428353 8356932
15 20225456 20641584
16 33046200 33175188
17 57970280 64939220
18 70011280 70039784
19 70677328 71241272
20 71362456 71425576
21 71824776 73894168
22 74014744 74705408
23 90487648 91228160
24 101058192 101967264
25 102176888 102230040
26 102409592 103123352
27 110591464 116044144
28 116407200 118512328
29 131185424 131616688
30 132395992 132461760
31 137564368 137927776
32 142716752 145888160
33 148377392 148480704
34 148838944 150553856
35 154876816 154971520
--
David Winsemius
More information about the R-help
mailing list