Taking the last element of the vectors from a list and create a new vector out of itHow to randomly select an item from a list?How do I remove an element from a list by index in Python?How to make a new List in JavaGetting the last element of a list in PythonHow to make a flat list out of list of lists?How do I get the number of elements in a list in Python?Is there a simple way to delete a list element by value?Simultaneously merge multiple data.frames in a listPrint the last row from a list of data framesWhy not inherit from List<T>?
The baby cries all morning
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
Lay out the Carpet
Was the picture area of a CRT a parallelogram (instead of a true rectangle)?
Mapping a list into a phase plot
Implement the Thanos sorting algorithm
Curses work by shouting - How to avoid collateral damage?
Applicability of Single Responsibility Principle
What is the term when two people sing in harmony, but they aren't singing the same notes?
How can I replace every global instance of "x[2]" with "x_2"
What is the intuitive meaning of having a linear relationship between the logs of two variables?
Can somebody explain Brexit in a few child-proof sentences?
Your magic is very sketchy
Minimal reference content
How to be diplomatic in refusing to write code that breaches the privacy of our users
Time travel short story where a man arrives in the late 19th century in a time machine and then sends the machine back into the past
Will it be accepted, if there is no ''Main Character" stereotype?
Star/Wye electrical connection math symbol
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
What to do with wrong results in talks?
Print name if parameter passed to function
Why are on-board computers allowed to change controls without notifying the pilots?
Ways to speed up user implemented RK4
Finding all intervals that match predicate in vector
Taking the last element of the vectors from a list and create a new vector out of it
How to randomly select an item from a list?How do I remove an element from a list by index in Python?How to make a new List in JavaGetting the last element of a list in PythonHow to make a flat list out of list of lists?How do I get the number of elements in a list in Python?Is there a simple way to delete a list element by value?Simultaneously merge multiple data.frames in a listPrint the last row from a list of data framesWhy not inherit from List<T>?
I want to create a data frame from the last elements of a list. I have describing two cases here, one easy and one a bit complecated.
Easy case
Let's assume a list or two vectors, v
v <- list("22" = c(2, 3, 5), "23" = c("aa", "bb"))
> str(v)
List of 2
$ 22: num [1:3] 2 3 5
$ 23: chr [1:2] "aa" "bb"
I want to have a data frame where in the first column I will have the element names (here 22 and 23) and in the second column I will have the last element of that vector.
I can write the following codes to generate what I want,
last_elems1 <- lapply(v, tail, n = 1L)
last_elems1 <- data.frame(last_elems1)
tidyr::gather(last_elems1)
> tidyr::gather(last_elems1)
key value
1 X22 5
2 X23 bb
I have the following questions here,
- How can I avoid the "X"s in the "key" column, e.i, I want only number 22, not X22.
Should I worry about the error message?
Warning message:
attributes are not identical across measure variables;
they will be dropped
Slightly complecated example
Here I have list of two (or more) data frames with two vectors inside. The list can be generated as follows,
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
> str(w)
List of 2
$ 22:'data.frame': 3 obs. of 2 variables:
..$ a: num [1:3] 2 3 5
..$ b: num [1:3] 5 6 8
$ 23:'data.frame': 2 obs. of 2 variables:
..$ a: num [1:2] 9 10
..$ b: num [1:2] 11 12
I want to have to last element of the variable b from each data frame inside the list. I am using the same codes described above, and that gives me the following,
last_elems2 <- lapply(w, tail, n = 1L)
last_elems2 <- data.frame(last_elems2)
tidyr::gather(last_elems2)
> tidyr::gather(last_elems2)
key value
1 X22.a 5
2 X22.b 8
3 X23.a 10
4 X23.b 12
Here are my questions,
- How to have the value in column
keylike22instead ofX22.b. - I want only the row
2and row4. That means, the output should be,
tidyr::gather(last_elems2)
key value
1 22 8
2 23 12
Any idea how to fine tune what I am doing?
r list
add a comment |
I want to create a data frame from the last elements of a list. I have describing two cases here, one easy and one a bit complecated.
Easy case
Let's assume a list or two vectors, v
v <- list("22" = c(2, 3, 5), "23" = c("aa", "bb"))
> str(v)
List of 2
$ 22: num [1:3] 2 3 5
$ 23: chr [1:2] "aa" "bb"
I want to have a data frame where in the first column I will have the element names (here 22 and 23) and in the second column I will have the last element of that vector.
I can write the following codes to generate what I want,
last_elems1 <- lapply(v, tail, n = 1L)
last_elems1 <- data.frame(last_elems1)
tidyr::gather(last_elems1)
> tidyr::gather(last_elems1)
key value
1 X22 5
2 X23 bb
I have the following questions here,
- How can I avoid the "X"s in the "key" column, e.i, I want only number 22, not X22.
Should I worry about the error message?
Warning message:
attributes are not identical across measure variables;
they will be dropped
Slightly complecated example
Here I have list of two (or more) data frames with two vectors inside. The list can be generated as follows,
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
> str(w)
List of 2
$ 22:'data.frame': 3 obs. of 2 variables:
..$ a: num [1:3] 2 3 5
..$ b: num [1:3] 5 6 8
$ 23:'data.frame': 2 obs. of 2 variables:
..$ a: num [1:2] 9 10
..$ b: num [1:2] 11 12
I want to have to last element of the variable b from each data frame inside the list. I am using the same codes described above, and that gives me the following,
last_elems2 <- lapply(w, tail, n = 1L)
last_elems2 <- data.frame(last_elems2)
tidyr::gather(last_elems2)
> tidyr::gather(last_elems2)
key value
1 X22.a 5
2 X22.b 8
3 X23.a 10
4 X23.b 12
Here are my questions,
- How to have the value in column
keylike22instead ofX22.b. - I want only the row
2and row4. That means, the output should be,
tidyr::gather(last_elems2)
key value
1 22 8
2 23 12
Any idea how to fine tune what I am doing?
r list
add a comment |
I want to create a data frame from the last elements of a list. I have describing two cases here, one easy and one a bit complecated.
Easy case
Let's assume a list or two vectors, v
v <- list("22" = c(2, 3, 5), "23" = c("aa", "bb"))
> str(v)
List of 2
$ 22: num [1:3] 2 3 5
$ 23: chr [1:2] "aa" "bb"
I want to have a data frame where in the first column I will have the element names (here 22 and 23) and in the second column I will have the last element of that vector.
I can write the following codes to generate what I want,
last_elems1 <- lapply(v, tail, n = 1L)
last_elems1 <- data.frame(last_elems1)
tidyr::gather(last_elems1)
> tidyr::gather(last_elems1)
key value
1 X22 5
2 X23 bb
I have the following questions here,
- How can I avoid the "X"s in the "key" column, e.i, I want only number 22, not X22.
Should I worry about the error message?
Warning message:
attributes are not identical across measure variables;
they will be dropped
Slightly complecated example
Here I have list of two (or more) data frames with two vectors inside. The list can be generated as follows,
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
> str(w)
List of 2
$ 22:'data.frame': 3 obs. of 2 variables:
..$ a: num [1:3] 2 3 5
..$ b: num [1:3] 5 6 8
$ 23:'data.frame': 2 obs. of 2 variables:
..$ a: num [1:2] 9 10
..$ b: num [1:2] 11 12
I want to have to last element of the variable b from each data frame inside the list. I am using the same codes described above, and that gives me the following,
last_elems2 <- lapply(w, tail, n = 1L)
last_elems2 <- data.frame(last_elems2)
tidyr::gather(last_elems2)
> tidyr::gather(last_elems2)
key value
1 X22.a 5
2 X22.b 8
3 X23.a 10
4 X23.b 12
Here are my questions,
- How to have the value in column
keylike22instead ofX22.b. - I want only the row
2and row4. That means, the output should be,
tidyr::gather(last_elems2)
key value
1 22 8
2 23 12
Any idea how to fine tune what I am doing?
r list
I want to create a data frame from the last elements of a list. I have describing two cases here, one easy and one a bit complecated.
Easy case
Let's assume a list or two vectors, v
v <- list("22" = c(2, 3, 5), "23" = c("aa", "bb"))
> str(v)
List of 2
$ 22: num [1:3] 2 3 5
$ 23: chr [1:2] "aa" "bb"
I want to have a data frame where in the first column I will have the element names (here 22 and 23) and in the second column I will have the last element of that vector.
I can write the following codes to generate what I want,
last_elems1 <- lapply(v, tail, n = 1L)
last_elems1 <- data.frame(last_elems1)
tidyr::gather(last_elems1)
> tidyr::gather(last_elems1)
key value
1 X22 5
2 X23 bb
I have the following questions here,
- How can I avoid the "X"s in the "key" column, e.i, I want only number 22, not X22.
Should I worry about the error message?
Warning message:
attributes are not identical across measure variables;
they will be dropped
Slightly complecated example
Here I have list of two (or more) data frames with two vectors inside. The list can be generated as follows,
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
> str(w)
List of 2
$ 22:'data.frame': 3 obs. of 2 variables:
..$ a: num [1:3] 2 3 5
..$ b: num [1:3] 5 6 8
$ 23:'data.frame': 2 obs. of 2 variables:
..$ a: num [1:2] 9 10
..$ b: num [1:2] 11 12
I want to have to last element of the variable b from each data frame inside the list. I am using the same codes described above, and that gives me the following,
last_elems2 <- lapply(w, tail, n = 1L)
last_elems2 <- data.frame(last_elems2)
tidyr::gather(last_elems2)
> tidyr::gather(last_elems2)
key value
1 X22.a 5
2 X22.b 8
3 X23.a 10
4 X23.b 12
Here are my questions,
- How to have the value in column
keylike22instead ofX22.b. - I want only the row
2and row4. That means, the output should be,
tidyr::gather(last_elems2)
key value
1 22 8
2 23 12
Any idea how to fine tune what I am doing?
r list
r list
edited Mar 8 at 9:47
markus
14.8k11336
14.8k11336
asked Mar 8 at 9:32
small_lebowskismall_lebowski
364316
364316
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A base R option
stack(lapply(w, function(x)
x[dim(x)[1], "b"] # return last element of column "b"
))
# values ind
#1 8 22
#2 12 23
When we use dim, we assume that w does not contain vectors - as a vector does not have a dimensions attribute. You would need to change that part to x[length(x)] then.
data
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060306%2ftaking-the-last-element-of-the-vectors-from-a-list-and-create-a-new-vector-out-o%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A base R option
stack(lapply(w, function(x)
x[dim(x)[1], "b"] # return last element of column "b"
))
# values ind
#1 8 22
#2 12 23
When we use dim, we assume that w does not contain vectors - as a vector does not have a dimensions attribute. You would need to change that part to x[length(x)] then.
data
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
add a comment |
A base R option
stack(lapply(w, function(x)
x[dim(x)[1], "b"] # return last element of column "b"
))
# values ind
#1 8 22
#2 12 23
When we use dim, we assume that w does not contain vectors - as a vector does not have a dimensions attribute. You would need to change that part to x[length(x)] then.
data
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
add a comment |
A base R option
stack(lapply(w, function(x)
x[dim(x)[1], "b"] # return last element of column "b"
))
# values ind
#1 8 22
#2 12 23
When we use dim, we assume that w does not contain vectors - as a vector does not have a dimensions attribute. You would need to change that part to x[length(x)] then.
data
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
A base R option
stack(lapply(w, function(x)
x[dim(x)[1], "b"] # return last element of column "b"
))
# values ind
#1 8 22
#2 12 23
When we use dim, we assume that w does not contain vectors - as a vector does not have a dimensions attribute. You would need to change that part to x[length(x)] then.
data
w <- list("22" = data.frame(a = c(2, 3, 5),
b = c(5, 6, 8)),
"23" = data.frame(a = c(9, 10),
b = c(11, 12))
)
edited Mar 8 at 9:56
answered Mar 8 at 9:43
markusmarkus
14.8k11336
14.8k11336
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060306%2ftaking-the-last-element-of-the-vectors-from-a-list-and-create-a-new-vector-out-o%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown