Implement lapply in conjunction with the get() function to vectorize merge of data tables? R
Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?
Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:
ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD")
for (i in 1:length(ticker_name)) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
dt <- merge(x = dt, y = meta, by= c("id"))
head(dt)
}
My non-working attempt with lapply:
lapply(
X = ticker_name,
FUN =
merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ticker_name)), by = c("id")
))
The error message:
Error in match.fun(FUN) :
c("'merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ' is not a function,
character or symbol", "' ticker_name)), by = c("id"))'
is not a function, character or symbol")
r merge get vectorization lapply
add a comment |
Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?
Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:
ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD")
for (i in 1:length(ticker_name)) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
dt <- merge(x = dt, y = meta, by= c("id"))
head(dt)
}
My non-working attempt with lapply:
lapply(
X = ticker_name,
FUN =
merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ticker_name)), by = c("id")
))
The error message:
Error in match.fun(FUN) :
c("'merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ' is not a function,
character or symbol", "' ticker_name)), by = c("id"))'
is not a function, character or symbol")
r merge get vectorization lapply
add a comment |
Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?
Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:
ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD")
for (i in 1:length(ticker_name)) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
dt <- merge(x = dt, y = meta, by= c("id"))
head(dt)
}
My non-working attempt with lapply:
lapply(
X = ticker_name,
FUN =
merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ticker_name)), by = c("id")
))
The error message:
Error in match.fun(FUN) :
c("'merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ' is not a function,
character or symbol", "' ticker_name)), by = c("id"))'
is not a function, character or symbol")
r merge get vectorization lapply
Question: How to implement the lapply function in conjunction with the get() function to merge a list of data tables?
Objective: For each of the elements in ticker_name, merge the data table called "dt_q_'ticker_name[i]'" and that called "meta_'ticker_name[i]'" by common "id" variable:
ticker_name <- c("CTNP", "PB", "SD", "PC", "PE", "TY", "XD")
for (i in 1:length(ticker_name)) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
dt <- merge(x = dt, y = meta, by= c("id"))
head(dt)
}
My non-working attempt with lapply:
lapply(
X = ticker_name,
FUN =
merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ticker_name)), by = c("id")
))
The error message:
Error in match.fun(FUN) :
c("'merge(x = get(paste0("dt_q_", ticker_name)),
y = get(paste0("meta_", ' is not a function,
character or symbol", "' ticker_name)), by = c("id"))'
is not a function, character or symbol")
r merge get vectorization lapply
r merge get vectorization lapply
edited Nov 27 '18 at 2:28
k1000x
asked Nov 26 '18 at 22:27
k1000xk1000x
386
386
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
We can use mget
to return all the objects into a list
and as the corresponding data.table should be merge
d, use Map
which can have multiple arguments
Map(merge, mget(paste0("dt_q_", ticker_name)),
mget(paste0("meta_", ticker_name)),
MoreArgs = list(by = 'id'))
Or using lapply
, loop through the 'ticker_name' then paste
the corresponding 'prefix' part, get
the values of the string objects and merge
lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)),
get(paste0("meta_", x)), by = 'id'))
NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is paste
ing the prefix with the whole 'ticker_name' which is not the case if we check the for
loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence
lapply(seq_along(ticker_name), function(i) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
merge(x = dt, y = meta, by= "id")
})
1
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
1
@k1000x Thank you. InMap
we could have used anonymous function likeMap(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used inMap
specifies the arguments ofmerge
(by = 'id'
). When we place it insidelist
, it will be a single unit that can be recycled for each element of thelist
(and it is the same argument for all the elements)
– akrun
Nov 27 '18 at 3:20
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%2f53490069%2fimplement-lapply-in-conjunction-with-the-get-function-to-vectorize-merge-of-da%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
We can use mget
to return all the objects into a list
and as the corresponding data.table should be merge
d, use Map
which can have multiple arguments
Map(merge, mget(paste0("dt_q_", ticker_name)),
mget(paste0("meta_", ticker_name)),
MoreArgs = list(by = 'id'))
Or using lapply
, loop through the 'ticker_name' then paste
the corresponding 'prefix' part, get
the values of the string objects and merge
lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)),
get(paste0("meta_", x)), by = 'id'))
NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is paste
ing the prefix with the whole 'ticker_name' which is not the case if we check the for
loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence
lapply(seq_along(ticker_name), function(i) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
merge(x = dt, y = meta, by= "id")
})
1
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
1
@k1000x Thank you. InMap
we could have used anonymous function likeMap(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used inMap
specifies the arguments ofmerge
(by = 'id'
). When we place it insidelist
, it will be a single unit that can be recycled for each element of thelist
(and it is the same argument for all the elements)
– akrun
Nov 27 '18 at 3:20
add a comment |
We can use mget
to return all the objects into a list
and as the corresponding data.table should be merge
d, use Map
which can have multiple arguments
Map(merge, mget(paste0("dt_q_", ticker_name)),
mget(paste0("meta_", ticker_name)),
MoreArgs = list(by = 'id'))
Or using lapply
, loop through the 'ticker_name' then paste
the corresponding 'prefix' part, get
the values of the string objects and merge
lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)),
get(paste0("meta_", x)), by = 'id'))
NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is paste
ing the prefix with the whole 'ticker_name' which is not the case if we check the for
loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence
lapply(seq_along(ticker_name), function(i) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
merge(x = dt, y = meta, by= "id")
})
1
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
1
@k1000x Thank you. InMap
we could have used anonymous function likeMap(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used inMap
specifies the arguments ofmerge
(by = 'id'
). When we place it insidelist
, it will be a single unit that can be recycled for each element of thelist
(and it is the same argument for all the elements)
– akrun
Nov 27 '18 at 3:20
add a comment |
We can use mget
to return all the objects into a list
and as the corresponding data.table should be merge
d, use Map
which can have multiple arguments
Map(merge, mget(paste0("dt_q_", ticker_name)),
mget(paste0("meta_", ticker_name)),
MoreArgs = list(by = 'id'))
Or using lapply
, loop through the 'ticker_name' then paste
the corresponding 'prefix' part, get
the values of the string objects and merge
lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)),
get(paste0("meta_", x)), by = 'id'))
NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is paste
ing the prefix with the whole 'ticker_name' which is not the case if we check the for
loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence
lapply(seq_along(ticker_name), function(i) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
merge(x = dt, y = meta, by= "id")
})
We can use mget
to return all the objects into a list
and as the corresponding data.table should be merge
d, use Map
which can have multiple arguments
Map(merge, mget(paste0("dt_q_", ticker_name)),
mget(paste0("meta_", ticker_name)),
MoreArgs = list(by = 'id'))
Or using lapply
, loop through the 'ticker_name' then paste
the corresponding 'prefix' part, get
the values of the string objects and merge
lapply(ticker_name, function(x) merge(get(paste0("dt_q_", x)),
get(paste0("meta_", x)), by = 'id'))
NOTE: In the OP's code, after looping through the 'ticker_name' ( or ticker_list - not clear), then it is paste
ing the prefix with the whole 'ticker_name' which is not the case if we check the for
loop where it is looping through the sequence of 'ticker_name'. We can also loop through the sequence
lapply(seq_along(ticker_name), function(i) {
dt <- get(paste0("dt_q_", ticker_name[i]))
meta <- get(paste0("meta_", ticker_name[i]))
merge(x = dt, y = meta, by= "id")
})
edited Nov 26 '18 at 22:44
answered Nov 26 '18 at 22:36
akrunakrun
409k13198273
409k13198273
1
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
1
@k1000x Thank you. InMap
we could have used anonymous function likeMap(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used inMap
specifies the arguments ofmerge
(by = 'id'
). When we place it insidelist
, it will be a single unit that can be recycled for each element of thelist
(and it is the same argument for all the elements)
– akrun
Nov 27 '18 at 3:20
add a comment |
1
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
1
@k1000x Thank you. InMap
we could have used anonymous function likeMap(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used inMap
specifies the arguments ofmerge
(by = 'id'
). When we place it insidelist
, it will be a single unit that can be recycled for each element of thelist
(and it is the same argument for all the elements)
– akrun
Nov 27 '18 at 3:20
1
1
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
Three great answers. Follow-up: In the first option, why do we need to include by = 'id' in a list?
– k1000x
Nov 27 '18 at 2:27
1
1
@k1000x Thank you. In
Map
we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used in Map
specifies the arguments of merge
(by = 'id'
). When we place it inside list
, it will be a single unit that can be recycled for each element of the list
(and it is the same argument for all the elements)– akrun
Nov 27 '18 at 3:20
@k1000x Thank you. In
Map
we could have used anonymous function like Map(function(x, y) merge(x, y, by = 'id'), mget(paste0("dt_q_", ticker_name)), mget(paste0("meta_", ticker_name)))
. That would make it easier to understand. The compact method used in Map
specifies the arguments of merge
(by = 'id'
). When we place it inside list
, it will be a single unit that can be recycled for each element of the list
(and it is the same argument for all the elements)– akrun
Nov 27 '18 at 3:20
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%2f53490069%2fimplement-lapply-in-conjunction-with-the-get-function-to-vectorize-merge-of-da%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