data.table does not modify a column as expected
I have a function
delete.all.after.pattern <- function (x,pattern) strsplit(x,pattern)[[1]][1]
and a data.table
a <- c(1:3)
b <- c("a","bn undesired text","c")
dt <- data.table(a=a, b=b)
Thus I expect that dt [, b:=delete.all.after.pattern(b,"\n")] would result in
a b
1: 1 a
2: 2 b
3: 3 c
instead of :
a b
1: 1 a
2: 2 a
3: 3 a
What am I missing?
r string data.table
add a comment |
I have a function
delete.all.after.pattern <- function (x,pattern) strsplit(x,pattern)[[1]][1]
and a data.table
a <- c(1:3)
b <- c("a","bn undesired text","c")
dt <- data.table(a=a, b=b)
Thus I expect that dt [, b:=delete.all.after.pattern(b,"\n")] would result in
a b
1: 1 a
2: 2 b
3: 3 c
instead of :
a b
1: 1 a
2: 2 a
3: 3 a
What am I missing?
r string data.table
add a comment |
I have a function
delete.all.after.pattern <- function (x,pattern) strsplit(x,pattern)[[1]][1]
and a data.table
a <- c(1:3)
b <- c("a","bn undesired text","c")
dt <- data.table(a=a, b=b)
Thus I expect that dt [, b:=delete.all.after.pattern(b,"\n")] would result in
a b
1: 1 a
2: 2 b
3: 3 c
instead of :
a b
1: 1 a
2: 2 a
3: 3 a
What am I missing?
r string data.table
I have a function
delete.all.after.pattern <- function (x,pattern) strsplit(x,pattern)[[1]][1]
and a data.table
a <- c(1:3)
b <- c("a","bn undesired text","c")
dt <- data.table(a=a, b=b)
Thus I expect that dt [, b:=delete.all.after.pattern(b,"\n")] would result in
a b
1: 1 a
2: 2 b
3: 3 c
instead of :
a b
1: 1 a
2: 2 a
3: 3 a
What am I missing?
r string data.table
r string data.table
edited Nov 22 '18 at 2:08
Ronak Shah
35.8k103856
35.8k103856
asked Nov 22 '18 at 1:44
Fabio CorreaFabio Correa
408
408
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you are looking for:
dt [, b := sapply(b, delete.all.after.pattern, pattern="\n")]
Your function is not vectorized and hence it only returns the first element, which is repeated for all rows.
Or you can call strsplit directly in j:
dt [, b := lapply(strsplit(b, "n"), `[[`, 1L)]
You can also put the code into a function and call it as well
fun <- function(x, p) lapply(strsplit(x, p), `[[`, 1L)
dt [, b := fun(b, "n")]
Another way is to use data.table::tstrsplit as follows:
dt[, b := tstrsplit(b, "\n", keep=1L)]
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
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%2f53422793%2fdata-table-does-not-modify-a-column-as-expected%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
I think you are looking for:
dt [, b := sapply(b, delete.all.after.pattern, pattern="\n")]
Your function is not vectorized and hence it only returns the first element, which is repeated for all rows.
Or you can call strsplit directly in j:
dt [, b := lapply(strsplit(b, "n"), `[[`, 1L)]
You can also put the code into a function and call it as well
fun <- function(x, p) lapply(strsplit(x, p), `[[`, 1L)
dt [, b := fun(b, "n")]
Another way is to use data.table::tstrsplit as follows:
dt[, b := tstrsplit(b, "\n", keep=1L)]
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
add a comment |
I think you are looking for:
dt [, b := sapply(b, delete.all.after.pattern, pattern="\n")]
Your function is not vectorized and hence it only returns the first element, which is repeated for all rows.
Or you can call strsplit directly in j:
dt [, b := lapply(strsplit(b, "n"), `[[`, 1L)]
You can also put the code into a function and call it as well
fun <- function(x, p) lapply(strsplit(x, p), `[[`, 1L)
dt [, b := fun(b, "n")]
Another way is to use data.table::tstrsplit as follows:
dt[, b := tstrsplit(b, "\n", keep=1L)]
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
add a comment |
I think you are looking for:
dt [, b := sapply(b, delete.all.after.pattern, pattern="\n")]
Your function is not vectorized and hence it only returns the first element, which is repeated for all rows.
Or you can call strsplit directly in j:
dt [, b := lapply(strsplit(b, "n"), `[[`, 1L)]
You can also put the code into a function and call it as well
fun <- function(x, p) lapply(strsplit(x, p), `[[`, 1L)
dt [, b := fun(b, "n")]
Another way is to use data.table::tstrsplit as follows:
dt[, b := tstrsplit(b, "\n", keep=1L)]
I think you are looking for:
dt [, b := sapply(b, delete.all.after.pattern, pattern="\n")]
Your function is not vectorized and hence it only returns the first element, which is repeated for all rows.
Or you can call strsplit directly in j:
dt [, b := lapply(strsplit(b, "n"), `[[`, 1L)]
You can also put the code into a function and call it as well
fun <- function(x, p) lapply(strsplit(x, p), `[[`, 1L)
dt [, b := fun(b, "n")]
Another way is to use data.table::tstrsplit as follows:
dt[, b := tstrsplit(b, "\n", keep=1L)]
edited Nov 22 '18 at 1:58
answered Nov 22 '18 at 1:53
chinsoon12chinsoon12
8,66111219
8,66111219
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
add a comment |
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
your full answer clarified me about other R strucure issues, thanks a lot.
– Fabio Correa
Nov 22 '18 at 10:27
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%2f53422793%2fdata-table-does-not-modify-a-column-as-expected%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