How to naming list elements using only lapply in R
This is my list:
l <- vector("list", 4)
l[[1]][1] <- list(c(1,2,3))
l[[1]][2] <- list(c(1,2,3))
l[[2]][1] <- list(c(1,2,3))
l[[2]][2] <- list(c(1,2,3))
l[[3]][1] <- list(c(1,2,3))
l[[3]][2] <- list(c(1,2,3))
l[[4]][1] <- list(c(1,2,3))
l[[4]][2] <- list(c(1,2,3))
I have this names vectors: mynames <- c("number one","number two")
How can I name the list elements using the lapply
function with the mynames
vector?
I tried this, but didn't work:
lapply(l, names(x) <- mynames)
Any help?
r apply lapply
add a comment |
This is my list:
l <- vector("list", 4)
l[[1]][1] <- list(c(1,2,3))
l[[1]][2] <- list(c(1,2,3))
l[[2]][1] <- list(c(1,2,3))
l[[2]][2] <- list(c(1,2,3))
l[[3]][1] <- list(c(1,2,3))
l[[3]][2] <- list(c(1,2,3))
l[[4]][1] <- list(c(1,2,3))
l[[4]][2] <- list(c(1,2,3))
I have this names vectors: mynames <- c("number one","number two")
How can I name the list elements using the lapply
function with the mynames
vector?
I tried this, but didn't work:
lapply(l, names(x) <- mynames)
Any help?
r apply lapply
1
You can use the functional form of a "special" function, something likelapply(l, `names<-`, mynames)
(which is equivalent tolapply(l, function(a) `names<-`(a, mynames))
which is equivalent tolapply(l, function(a) { names(a) <- mynames; a; })
).
– r2evans
Nov 12 '18 at 23:05
@r2evans many thanks. This is amazing. I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read?
– Laura
Nov 12 '18 at 23:11
1
When you do something likenames(x) <- c('a','b','c')
, there is a function callednames
which only returns the names of the objectx
, and there is a function callednames<-
(no space) that is called whennames(x)
is on the left side of an assignment operator (<-
or=
). Most functions do not have this assignment equivalent; more the point, it does not make sense for most functions to do something on the assignment-side (left-hand side). There are some other minor differences, not really notable here. BTW: I think @Parfait's suggestion to usesetNames
is simpler and just as good.
– r2evans
Nov 12 '18 at 23:29
add a comment |
This is my list:
l <- vector("list", 4)
l[[1]][1] <- list(c(1,2,3))
l[[1]][2] <- list(c(1,2,3))
l[[2]][1] <- list(c(1,2,3))
l[[2]][2] <- list(c(1,2,3))
l[[3]][1] <- list(c(1,2,3))
l[[3]][2] <- list(c(1,2,3))
l[[4]][1] <- list(c(1,2,3))
l[[4]][2] <- list(c(1,2,3))
I have this names vectors: mynames <- c("number one","number two")
How can I name the list elements using the lapply
function with the mynames
vector?
I tried this, but didn't work:
lapply(l, names(x) <- mynames)
Any help?
r apply lapply
This is my list:
l <- vector("list", 4)
l[[1]][1] <- list(c(1,2,3))
l[[1]][2] <- list(c(1,2,3))
l[[2]][1] <- list(c(1,2,3))
l[[2]][2] <- list(c(1,2,3))
l[[3]][1] <- list(c(1,2,3))
l[[3]][2] <- list(c(1,2,3))
l[[4]][1] <- list(c(1,2,3))
l[[4]][2] <- list(c(1,2,3))
I have this names vectors: mynames <- c("number one","number two")
How can I name the list elements using the lapply
function with the mynames
vector?
I tried this, but didn't work:
lapply(l, names(x) <- mynames)
Any help?
r apply lapply
r apply lapply
edited Nov 12 '18 at 23:10
Florian
1,072817
1,072817
asked Nov 12 '18 at 22:56
LauraLaura
35319
35319
1
You can use the functional form of a "special" function, something likelapply(l, `names<-`, mynames)
(which is equivalent tolapply(l, function(a) `names<-`(a, mynames))
which is equivalent tolapply(l, function(a) { names(a) <- mynames; a; })
).
– r2evans
Nov 12 '18 at 23:05
@r2evans many thanks. This is amazing. I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read?
– Laura
Nov 12 '18 at 23:11
1
When you do something likenames(x) <- c('a','b','c')
, there is a function callednames
which only returns the names of the objectx
, and there is a function callednames<-
(no space) that is called whennames(x)
is on the left side of an assignment operator (<-
or=
). Most functions do not have this assignment equivalent; more the point, it does not make sense for most functions to do something on the assignment-side (left-hand side). There are some other minor differences, not really notable here. BTW: I think @Parfait's suggestion to usesetNames
is simpler and just as good.
– r2evans
Nov 12 '18 at 23:29
add a comment |
1
You can use the functional form of a "special" function, something likelapply(l, `names<-`, mynames)
(which is equivalent tolapply(l, function(a) `names<-`(a, mynames))
which is equivalent tolapply(l, function(a) { names(a) <- mynames; a; })
).
– r2evans
Nov 12 '18 at 23:05
@r2evans many thanks. This is amazing. I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read?
– Laura
Nov 12 '18 at 23:11
1
When you do something likenames(x) <- c('a','b','c')
, there is a function callednames
which only returns the names of the objectx
, and there is a function callednames<-
(no space) that is called whennames(x)
is on the left side of an assignment operator (<-
or=
). Most functions do not have this assignment equivalent; more the point, it does not make sense for most functions to do something on the assignment-side (left-hand side). There are some other minor differences, not really notable here. BTW: I think @Parfait's suggestion to usesetNames
is simpler and just as good.
– r2evans
Nov 12 '18 at 23:29
1
1
You can use the functional form of a "special" function, something like
lapply(l, `names<-`, mynames)
(which is equivalent to lapply(l, function(a) `names<-`(a, mynames))
which is equivalent to lapply(l, function(a) { names(a) <- mynames; a; })
).– r2evans
Nov 12 '18 at 23:05
You can use the functional form of a "special" function, something like
lapply(l, `names<-`, mynames)
(which is equivalent to lapply(l, function(a) `names<-`(a, mynames))
which is equivalent to lapply(l, function(a) { names(a) <- mynames; a; })
).– r2evans
Nov 12 '18 at 23:05
@r2evans many thanks. This is amazing. I am a begginer in lapply. How can I understand more this sintax such as
names<-
? I had no idea that I could use this in lapply. Any suggestion to read?– Laura
Nov 12 '18 at 23:11
@r2evans many thanks. This is amazing. I am a begginer in lapply. How can I understand more this sintax such as
names<-
? I had no idea that I could use this in lapply. Any suggestion to read?– Laura
Nov 12 '18 at 23:11
1
1
When you do something like
names(x) <- c('a','b','c')
, there is a function called names
which only returns the names of the object x
, and there is a function called names<-
(no space) that is called when names(x)
is on the left side of an assignment operator (<-
or =
). Most functions do not have this assignment equivalent; more the point, it does not make sense for most functions to do something on the assignment-side (left-hand side). There are some other minor differences, not really notable here. BTW: I think @Parfait's suggestion to use setNames
is simpler and just as good.– r2evans
Nov 12 '18 at 23:29
When you do something like
names(x) <- c('a','b','c')
, there is a function called names
which only returns the names of the object x
, and there is a function called names<-
(no space) that is called when names(x)
is on the left side of an assignment operator (<-
or =
). Most functions do not have this assignment equivalent; more the point, it does not make sense for most functions to do something on the assignment-side (left-hand side). There are some other minor differences, not really notable here. BTW: I think @Parfait's suggestion to use setNames
is simpler and just as good.– r2evans
Nov 12 '18 at 23:29
add a comment |
1 Answer
1
active
oldest
votes
The second argument of lappyl()
has to be a function. One can use setNames()
:
named_list <- lapply(l, setNames, nm=mynames)
named_list[1:2]
[[1]]
[[1]]$`number one`
[1] 1 2 3
[[1]]$`number two`
[1] 1 2 3
[[2]]
[[2]]$`number one`
[1] 1 2 3
[[2]]$`number two`
[1] 1 2 3
An alternative version based on the replacement function `names<-`
is:
named_list2 <- lapply(l, function(x, names) { names(x) <- names; x },
names=mynames)
identical(named_list, named_list2)
[1] TRUE
1
I assumed that Laura would prefer to not discard the data when naming it. Perhaps returna
from the inner function?
– r2evans
Nov 12 '18 at 23:06
1
Nice preservation of scope by adding thenames=
argument.
– r2evans
Nov 12 '18 at 23:07
1
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintaxnames<-
has any name?
– Laura
Nov 12 '18 at 23:15
2
Consider even the left hand function,setNames
:lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
|
show 2 more comments
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%2f53271294%2fhow-to-naming-list-elements-using-only-lapply-in-r%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
The second argument of lappyl()
has to be a function. One can use setNames()
:
named_list <- lapply(l, setNames, nm=mynames)
named_list[1:2]
[[1]]
[[1]]$`number one`
[1] 1 2 3
[[1]]$`number two`
[1] 1 2 3
[[2]]
[[2]]$`number one`
[1] 1 2 3
[[2]]$`number two`
[1] 1 2 3
An alternative version based on the replacement function `names<-`
is:
named_list2 <- lapply(l, function(x, names) { names(x) <- names; x },
names=mynames)
identical(named_list, named_list2)
[1] TRUE
1
I assumed that Laura would prefer to not discard the data when naming it. Perhaps returna
from the inner function?
– r2evans
Nov 12 '18 at 23:06
1
Nice preservation of scope by adding thenames=
argument.
– r2evans
Nov 12 '18 at 23:07
1
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintaxnames<-
has any name?
– Laura
Nov 12 '18 at 23:15
2
Consider even the left hand function,setNames
:lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
|
show 2 more comments
The second argument of lappyl()
has to be a function. One can use setNames()
:
named_list <- lapply(l, setNames, nm=mynames)
named_list[1:2]
[[1]]
[[1]]$`number one`
[1] 1 2 3
[[1]]$`number two`
[1] 1 2 3
[[2]]
[[2]]$`number one`
[1] 1 2 3
[[2]]$`number two`
[1] 1 2 3
An alternative version based on the replacement function `names<-`
is:
named_list2 <- lapply(l, function(x, names) { names(x) <- names; x },
names=mynames)
identical(named_list, named_list2)
[1] TRUE
1
I assumed that Laura would prefer to not discard the data when naming it. Perhaps returna
from the inner function?
– r2evans
Nov 12 '18 at 23:06
1
Nice preservation of scope by adding thenames=
argument.
– r2evans
Nov 12 '18 at 23:07
1
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintaxnames<-
has any name?
– Laura
Nov 12 '18 at 23:15
2
Consider even the left hand function,setNames
:lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
|
show 2 more comments
The second argument of lappyl()
has to be a function. One can use setNames()
:
named_list <- lapply(l, setNames, nm=mynames)
named_list[1:2]
[[1]]
[[1]]$`number one`
[1] 1 2 3
[[1]]$`number two`
[1] 1 2 3
[[2]]
[[2]]$`number one`
[1] 1 2 3
[[2]]$`number two`
[1] 1 2 3
An alternative version based on the replacement function `names<-`
is:
named_list2 <- lapply(l, function(x, names) { names(x) <- names; x },
names=mynames)
identical(named_list, named_list2)
[1] TRUE
The second argument of lappyl()
has to be a function. One can use setNames()
:
named_list <- lapply(l, setNames, nm=mynames)
named_list[1:2]
[[1]]
[[1]]$`number one`
[1] 1 2 3
[[1]]$`number two`
[1] 1 2 3
[[2]]
[[2]]$`number one`
[1] 1 2 3
[[2]]$`number two`
[1] 1 2 3
An alternative version based on the replacement function `names<-`
is:
named_list2 <- lapply(l, function(x, names) { names(x) <- names; x },
names=mynames)
identical(named_list, named_list2)
[1] TRUE
edited Nov 13 '18 at 3:31
answered Nov 12 '18 at 23:05
FlorianFlorian
1,072817
1,072817
1
I assumed that Laura would prefer to not discard the data when naming it. Perhaps returna
from the inner function?
– r2evans
Nov 12 '18 at 23:06
1
Nice preservation of scope by adding thenames=
argument.
– r2evans
Nov 12 '18 at 23:07
1
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintaxnames<-
has any name?
– Laura
Nov 12 '18 at 23:15
2
Consider even the left hand function,setNames
:lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
|
show 2 more comments
1
I assumed that Laura would prefer to not discard the data when naming it. Perhaps returna
from the inner function?
– r2evans
Nov 12 '18 at 23:06
1
Nice preservation of scope by adding thenames=
argument.
– r2evans
Nov 12 '18 at 23:07
1
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such asnames<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintaxnames<-
has any name?
– Laura
Nov 12 '18 at 23:15
2
Consider even the left hand function,setNames
:lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
1
1
I assumed that Laura would prefer to not discard the data when naming it. Perhaps return
a
from the inner function?– r2evans
Nov 12 '18 at 23:06
I assumed that Laura would prefer to not discard the data when naming it. Perhaps return
a
from the inner function?– r2evans
Nov 12 '18 at 23:06
1
1
Nice preservation of scope by adding the
names=
argument.– r2evans
Nov 12 '18 at 23:07
Nice preservation of scope by adding the
names=
argument.– r2evans
Nov 12 '18 at 23:07
1
1
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@r2evans thanks for the comment. I updated the answer.
– Florian
Nov 12 '18 at 23:14
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such as
names<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintax names<-
has any name?– Laura
Nov 12 '18 at 23:15
@Florian many thanks. @r2evans many thanks. As I said, I am a begginer in lapply. How can I understand more this sintax such as
names<-
? I had no idea that I could use this in lapply. Any suggestion to read? This sintax names<-
has any name?– Laura
Nov 12 '18 at 23:15
2
2
Consider even the left hand function,
setNames
: lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
Consider even the left hand function,
setNames
: lapply(l, function(x) setNames(x, mynames))
– Parfait
Nov 12 '18 at 23:20
|
show 2 more comments
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%2f53271294%2fhow-to-naming-list-elements-using-only-lapply-in-r%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
1
You can use the functional form of a "special" function, something like
lapply(l, `names<-`, mynames)
(which is equivalent tolapply(l, function(a) `names<-`(a, mynames))
which is equivalent tolapply(l, function(a) { names(a) <- mynames; a; })
).– r2evans
Nov 12 '18 at 23:05
@r2evans many thanks. This is amazing. I am a begginer in lapply. How can I understand more this sintax such as
names<-
? I had no idea that I could use this in lapply. Any suggestion to read?– Laura
Nov 12 '18 at 23:11
1
When you do something like
names(x) <- c('a','b','c')
, there is a function callednames
which only returns the names of the objectx
, and there is a function callednames<-
(no space) that is called whennames(x)
is on the left side of an assignment operator (<-
or=
). Most functions do not have this assignment equivalent; more the point, it does not make sense for most functions to do something on the assignment-side (left-hand side). There are some other minor differences, not really notable here. BTW: I think @Parfait's suggestion to usesetNames
is simpler and just as good.– r2evans
Nov 12 '18 at 23:29