Lapply Giving List of List Results
I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:
df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}
df$X11<-lapply(df$X1, dummy_fn)
summary(df$X11)
Here is the result I'm getting:
How can I make the results just normal 0 and 1 integers?
r lapply
add a comment |
I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:
df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}
df$X11<-lapply(df$X1, dummy_fn)
summary(df$X11)
Here is the result I'm getting:
How can I make the results just normal 0 and 1 integers?
r lapply
Maybe you'd prefersapply
(or for some more type safety,vapply
). I assume your real code is more complicated, otherwise a simpleifelse
would probably suffice in this instance.
– joran
Nov 26 '18 at 18:58
df$X11
is a list and it will summarize each part of the list and not the contents of the list
– Mike
Nov 26 '18 at 19:00
add a comment |
I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:
df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}
df$X11<-lapply(df$X1, dummy_fn)
summary(df$X11)
Here is the result I'm getting:
How can I make the results just normal 0 and 1 integers?
r lapply
I'm having some problems when I try to apply a function to a column. I thought I was doing everything correctly, but it is giving me some pretty insane results. Here is the code:
df <- data.frame(replicate(10,sample(0:10,10,rep=TRUE)))
dummy_fn <-function(col_name){
if (col_name>5){
return(1)
}
else{
return(0)
}
}
df$X11<-lapply(df$X1, dummy_fn)
summary(df$X11)
Here is the result I'm getting:
How can I make the results just normal 0 and 1 integers?
r lapply
r lapply
edited Nov 26 '18 at 19:51
hrbrmstr
61.1k690151
61.1k690151
asked Nov 26 '18 at 18:56
Kevin GregoryKevin Gregory
102
102
Maybe you'd prefersapply
(or for some more type safety,vapply
). I assume your real code is more complicated, otherwise a simpleifelse
would probably suffice in this instance.
– joran
Nov 26 '18 at 18:58
df$X11
is a list and it will summarize each part of the list and not the contents of the list
– Mike
Nov 26 '18 at 19:00
add a comment |
Maybe you'd prefersapply
(or for some more type safety,vapply
). I assume your real code is more complicated, otherwise a simpleifelse
would probably suffice in this instance.
– joran
Nov 26 '18 at 18:58
df$X11
is a list and it will summarize each part of the list and not the contents of the list
– Mike
Nov 26 '18 at 19:00
Maybe you'd prefer
sapply
(or for some more type safety, vapply
). I assume your real code is more complicated, otherwise a simple ifelse
would probably suffice in this instance.– joran
Nov 26 '18 at 18:58
Maybe you'd prefer
sapply
(or for some more type safety, vapply
). I assume your real code is more complicated, otherwise a simple ifelse
would probably suffice in this instance.– joran
Nov 26 '18 at 18:58
df$X11
is a list and it will summarize each part of the list and not the contents of the list– Mike
Nov 26 '18 at 19:00
df$X11
is a list and it will summarize each part of the list and not the contents of the list– Mike
Nov 26 '18 at 19:00
add a comment |
2 Answers
2
active
oldest
votes
Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))
EDIT:
lapply
returns a list. you can unlist
the results after using lapply
as the step above suggests, and that would work.
Alternatively, you may use sapply
instead. sapply
simplifies lapply
and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply
). use it with caution, and double check the class of the returned results.
In this case, it is able to simplify the results
df$X11<-sapply(df$X1, dummy_fn) #Use sapply
summary(df$X11)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.25 1.00 0.70 1.00 1.00
add a comment |
We don't need an lapply
for this
dummy_fn <- function(data, columnName) {
as.integer(data[[columnName]] > 5)
}
df$X11 <- dummy_fn(df, 'X1')
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%2f53487373%2flapply-giving-list-of-list-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))
EDIT:
lapply
returns a list. you can unlist
the results after using lapply
as the step above suggests, and that would work.
Alternatively, you may use sapply
instead. sapply
simplifies lapply
and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply
). use it with caution, and double check the class of the returned results.
In this case, it is able to simplify the results
df$X11<-sapply(df$X1, dummy_fn) #Use sapply
summary(df$X11)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.25 1.00 0.70 1.00 1.00
add a comment |
Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))
EDIT:
lapply
returns a list. you can unlist
the results after using lapply
as the step above suggests, and that would work.
Alternatively, you may use sapply
instead. sapply
simplifies lapply
and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply
). use it with caution, and double check the class of the returned results.
In this case, it is able to simplify the results
df$X11<-sapply(df$X1, dummy_fn) #Use sapply
summary(df$X11)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.25 1.00 0.70 1.00 1.00
add a comment |
Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))
EDIT:
lapply
returns a list. you can unlist
the results after using lapply
as the step above suggests, and that would work.
Alternatively, you may use sapply
instead. sapply
simplifies lapply
and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply
). use it with caution, and double check the class of the returned results.
In this case, it is able to simplify the results
df$X11<-sapply(df$X1, dummy_fn) #Use sapply
summary(df$X11)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.25 1.00 0.70 1.00 1.00
Try unlist. df$X11 <- unlist(lapply(df$X1, dummy_fn))
EDIT:
lapply
returns a list. you can unlist
the results after using lapply
as the step above suggests, and that would work.
Alternatively, you may use sapply
instead. sapply
simplifies lapply
and returns a vector or matrix. Basically, it will unlist the result if it can. Otherwise it will return a list (it will behave same as lapply
). use it with caution, and double check the class of the returned results.
In this case, it is able to simplify the results
df$X11<-sapply(df$X1, dummy_fn) #Use sapply
summary(df$X11)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.25 1.00 0.70 1.00 1.00
edited Nov 26 '18 at 21:48
Wally Ali
2,152917
2,152917
answered Nov 26 '18 at 19:06
pooja ppooja p
1297
1297
add a comment |
add a comment |
We don't need an lapply
for this
dummy_fn <- function(data, columnName) {
as.integer(data[[columnName]] > 5)
}
df$X11 <- dummy_fn(df, 'X1')
add a comment |
We don't need an lapply
for this
dummy_fn <- function(data, columnName) {
as.integer(data[[columnName]] > 5)
}
df$X11 <- dummy_fn(df, 'X1')
add a comment |
We don't need an lapply
for this
dummy_fn <- function(data, columnName) {
as.integer(data[[columnName]] > 5)
}
df$X11 <- dummy_fn(df, 'X1')
We don't need an lapply
for this
dummy_fn <- function(data, columnName) {
as.integer(data[[columnName]] > 5)
}
df$X11 <- dummy_fn(df, 'X1')
answered Nov 26 '18 at 19:00
akrunakrun
408k13198273
408k13198273
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%2f53487373%2flapply-giving-list-of-list-results%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
Maybe you'd prefer
sapply
(or for some more type safety,vapply
). I assume your real code is more complicated, otherwise a simpleifelse
would probably suffice in this instance.– joran
Nov 26 '18 at 18:58
df$X11
is a list and it will summarize each part of the list and not the contents of the list– Mike
Nov 26 '18 at 19:00