How to assign list as observation in data table?











up vote
3
down vote

favorite
2












I have a data table that contains character observations:



library(data.table)
library(stringr)

DT = data.table(strings = c('AAABD', 'BBDA', 'AACBDAA', 'ABACD'))


I would like to create a variable that contains counts of 'A', 'AA', and 'AAA' in each observation as a list. To do this I have created a function foo:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- list('n' = n, 'n2' = n2, 'n3' = n3)

return(df)
}


I apply this function to DT to create a new variable for count observation as a list:



DT[, count := foo(strings)]


When I do this, I receive this error:



Warning message:
In `[.data.table`(DT, , `:=`(counts, foo(strings))) :
Supplied 3 items to be assigned to 4 items of column 'counts' (recycled leaving remainder of 1 items).


The data table returned has count variable lists of size 4 instead of size 3 and does not represent the amount of 'A', 'AA', and 'AAA' accurately for each string observation in variable strings. How can I assign a list as an observation in a data table?










share|improve this question






















  • I understand what you're trying to do. However, it is unclear to me how you want you output? Can you provide or describe your wanted output?
    – Anders Ellern Bilgrau
    Nov 19 at 22:12















up vote
3
down vote

favorite
2












I have a data table that contains character observations:



library(data.table)
library(stringr)

DT = data.table(strings = c('AAABD', 'BBDA', 'AACBDAA', 'ABACD'))


I would like to create a variable that contains counts of 'A', 'AA', and 'AAA' in each observation as a list. To do this I have created a function foo:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- list('n' = n, 'n2' = n2, 'n3' = n3)

return(df)
}


I apply this function to DT to create a new variable for count observation as a list:



DT[, count := foo(strings)]


When I do this, I receive this error:



Warning message:
In `[.data.table`(DT, , `:=`(counts, foo(strings))) :
Supplied 3 items to be assigned to 4 items of column 'counts' (recycled leaving remainder of 1 items).


The data table returned has count variable lists of size 4 instead of size 3 and does not represent the amount of 'A', 'AA', and 'AAA' accurately for each string observation in variable strings. How can I assign a list as an observation in a data table?










share|improve this question






















  • I understand what you're trying to do. However, it is unclear to me how you want you output? Can you provide or describe your wanted output?
    – Anders Ellern Bilgrau
    Nov 19 at 22:12













up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





I have a data table that contains character observations:



library(data.table)
library(stringr)

DT = data.table(strings = c('AAABD', 'BBDA', 'AACBDAA', 'ABACD'))


I would like to create a variable that contains counts of 'A', 'AA', and 'AAA' in each observation as a list. To do this I have created a function foo:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- list('n' = n, 'n2' = n2, 'n3' = n3)

return(df)
}


I apply this function to DT to create a new variable for count observation as a list:



DT[, count := foo(strings)]


When I do this, I receive this error:



Warning message:
In `[.data.table`(DT, , `:=`(counts, foo(strings))) :
Supplied 3 items to be assigned to 4 items of column 'counts' (recycled leaving remainder of 1 items).


The data table returned has count variable lists of size 4 instead of size 3 and does not represent the amount of 'A', 'AA', and 'AAA' accurately for each string observation in variable strings. How can I assign a list as an observation in a data table?










share|improve this question













I have a data table that contains character observations:



library(data.table)
library(stringr)

DT = data.table(strings = c('AAABD', 'BBDA', 'AACBDAA', 'ABACD'))


I would like to create a variable that contains counts of 'A', 'AA', and 'AAA' in each observation as a list. To do this I have created a function foo:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- list('n' = n, 'n2' = n2, 'n3' = n3)

return(df)
}


I apply this function to DT to create a new variable for count observation as a list:



DT[, count := foo(strings)]


When I do this, I receive this error:



Warning message:
In `[.data.table`(DT, , `:=`(counts, foo(strings))) :
Supplied 3 items to be assigned to 4 items of column 'counts' (recycled leaving remainder of 1 items).


The data table returned has count variable lists of size 4 instead of size 3 and does not represent the amount of 'A', 'AA', and 'AAA' accurately for each string observation in variable strings. How can I assign a list as an observation in a data table?







r dataframe data.table






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:08









Avlayort

183




183












  • I understand what you're trying to do. However, it is unclear to me how you want you output? Can you provide or describe your wanted output?
    – Anders Ellern Bilgrau
    Nov 19 at 22:12


















  • I understand what you're trying to do. However, it is unclear to me how you want you output? Can you provide or describe your wanted output?
    – Anders Ellern Bilgrau
    Nov 19 at 22:12
















I understand what you're trying to do. However, it is unclear to me how you want you output? Can you provide or describe your wanted output?
– Anders Ellern Bilgrau
Nov 19 at 22:12




I understand what you're trying to do. However, it is unclear to me how you want you output? Can you provide or describe your wanted output?
– Anders Ellern Bilgrau
Nov 19 at 22:12












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










You need to transpose the list:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- transpose(list('n' = n, 'n2' = n2, 'n3' = n3)) # <- add transpose
return(df)
}

DT[, count := foo(strings)]
DT
# strings count
# 1: AAABD 3,1,1
# 2: BBDA 1,0,0
# 3: AACBDAA 4,2,0
# 4: ABACD 2,0,0

str(DT)
# Classes ‘data.table’ and 'data.frame': 4 obs. of 2 variables:
# $ strings: chr "AAABD" "BBDA" "AACBDAA" "ABACD"
# $ count :List of 4
# ..$ : int 3 1 1
# ..$ : int 1 0 0
# ..$ : int 4 2 0
# ..$ : int 2 0 0





share|improve this answer























  • Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
    – Avlayort
    Nov 20 at 18:17










  • I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
    – Avlayort
    Nov 20 at 19:03






  • 2




    For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
    – Henrik
    Nov 20 at 19:14










  • Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
    – Avlayort
    Nov 20 at 20:01




















up vote
3
down vote













Assigning a list of columns to a single name can result in the warning message. Instead it can be



DT[, c('n', 'n2', 'n3') := .(str_count(strings, 'A'), 
str_count(strings, 'AA'), str_count(strings, 'AAA'))]





share|improve this answer

















  • 1




    Surely there is an existing Q&A for this? (I searched but couldn't find one)
    – smci
    Nov 19 at 22:46













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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383344%2fhow-to-assign-list-as-observation-in-data-table%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








up vote
3
down vote



accepted










You need to transpose the list:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- transpose(list('n' = n, 'n2' = n2, 'n3' = n3)) # <- add transpose
return(df)
}

DT[, count := foo(strings)]
DT
# strings count
# 1: AAABD 3,1,1
# 2: BBDA 1,0,0
# 3: AACBDAA 4,2,0
# 4: ABACD 2,0,0

str(DT)
# Classes ‘data.table’ and 'data.frame': 4 obs. of 2 variables:
# $ strings: chr "AAABD" "BBDA" "AACBDAA" "ABACD"
# $ count :List of 4
# ..$ : int 3 1 1
# ..$ : int 1 0 0
# ..$ : int 4 2 0
# ..$ : int 2 0 0





share|improve this answer























  • Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
    – Avlayort
    Nov 20 at 18:17










  • I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
    – Avlayort
    Nov 20 at 19:03






  • 2




    For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
    – Henrik
    Nov 20 at 19:14










  • Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
    – Avlayort
    Nov 20 at 20:01

















up vote
3
down vote



accepted










You need to transpose the list:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- transpose(list('n' = n, 'n2' = n2, 'n3' = n3)) # <- add transpose
return(df)
}

DT[, count := foo(strings)]
DT
# strings count
# 1: AAABD 3,1,1
# 2: BBDA 1,0,0
# 3: AACBDAA 4,2,0
# 4: ABACD 2,0,0

str(DT)
# Classes ‘data.table’ and 'data.frame': 4 obs. of 2 variables:
# $ strings: chr "AAABD" "BBDA" "AACBDAA" "ABACD"
# $ count :List of 4
# ..$ : int 3 1 1
# ..$ : int 1 0 0
# ..$ : int 4 2 0
# ..$ : int 2 0 0





share|improve this answer























  • Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
    – Avlayort
    Nov 20 at 18:17










  • I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
    – Avlayort
    Nov 20 at 19:03






  • 2




    For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
    – Henrik
    Nov 20 at 19:14










  • Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
    – Avlayort
    Nov 20 at 20:01















up vote
3
down vote



accepted







up vote
3
down vote



accepted






You need to transpose the list:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- transpose(list('n' = n, 'n2' = n2, 'n3' = n3)) # <- add transpose
return(df)
}

DT[, count := foo(strings)]
DT
# strings count
# 1: AAABD 3,1,1
# 2: BBDA 1,0,0
# 3: AACBDAA 4,2,0
# 4: ABACD 2,0,0

str(DT)
# Classes ‘data.table’ and 'data.frame': 4 obs. of 2 variables:
# $ strings: chr "AAABD" "BBDA" "AACBDAA" "ABACD"
# $ count :List of 4
# ..$ : int 3 1 1
# ..$ : int 1 0 0
# ..$ : int 4 2 0
# ..$ : int 2 0 0





share|improve this answer














You need to transpose the list:



foo <- function(str) {
n <- str_count(str, 'A')
n2 <- str_count(str, 'AA')
n3 <- str_count(str, 'AAA')
df <- transpose(list('n' = n, 'n2' = n2, 'n3' = n3)) # <- add transpose
return(df)
}

DT[, count := foo(strings)]
DT
# strings count
# 1: AAABD 3,1,1
# 2: BBDA 1,0,0
# 3: AACBDAA 4,2,0
# 4: ABACD 2,0,0

str(DT)
# Classes ‘data.table’ and 'data.frame': 4 obs. of 2 variables:
# $ strings: chr "AAABD" "BBDA" "AACBDAA" "ABACD"
# $ count :List of 4
# ..$ : int 3 1 1
# ..$ : int 1 0 0
# ..$ : int 4 2 0
# ..$ : int 2 0 0






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 22:52

























answered Nov 19 at 22:26









Henrik

40.4k991107




40.4k991107












  • Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
    – Avlayort
    Nov 20 at 18:17










  • I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
    – Avlayort
    Nov 20 at 19:03






  • 2




    For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
    – Henrik
    Nov 20 at 19:14










  • Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
    – Avlayort
    Nov 20 at 20:01




















  • Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
    – Avlayort
    Nov 20 at 18:17










  • I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
    – Avlayort
    Nov 20 at 19:03






  • 2




    For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
    – Henrik
    Nov 20 at 19:14










  • Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
    – Avlayort
    Nov 20 at 20:01


















Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
– Avlayort
Nov 20 at 18:17




Thank you, this is exactly the solution I was looking for! As a follow up question, is it possible to subset an item of the list from the data table to use in ggplot? Thank you.
– Avlayort
Nov 20 at 18:17












I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
– Avlayort
Nov 20 at 19:03




I would like to be able to plot the first item (or second or third) of the list in count against another variable in my data set. In the data set I am working with, it is useful to have count as a list for simplicity of statistical analysis but also because it makes the data set less wieldy and easier to peruse for data exploration (I have over 100 variables in my data set). Having it as a list, however, has posed a new challenge in subsetting specific items in the list to use as a variable to plot against.
– Avlayort
Nov 20 at 19:03




2




2




For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
– Henrik
Nov 20 at 19:14




For plotting (especially with ggplot) I think you should avoid list columns and rather keep your data tidy (tidier), with one observation per row. That said, to extract the values of a certain row, e.g. 3, as a vector: DT[ , count[[3]]] or unlist(DT[3]$count).
– Henrik
Nov 20 at 19:14












Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
– Avlayort
Nov 20 at 20:01






Thank you for your help, your answer was able to help me determine the solution I was looking for: ggplot(data = DT, aes(x = unlist(lapply(count, '[', 1)), y = unlist(lapply(count, '[', 2)))) + geom_point()
– Avlayort
Nov 20 at 20:01














up vote
3
down vote













Assigning a list of columns to a single name can result in the warning message. Instead it can be



DT[, c('n', 'n2', 'n3') := .(str_count(strings, 'A'), 
str_count(strings, 'AA'), str_count(strings, 'AAA'))]





share|improve this answer

















  • 1




    Surely there is an existing Q&A for this? (I searched but couldn't find one)
    – smci
    Nov 19 at 22:46

















up vote
3
down vote













Assigning a list of columns to a single name can result in the warning message. Instead it can be



DT[, c('n', 'n2', 'n3') := .(str_count(strings, 'A'), 
str_count(strings, 'AA'), str_count(strings, 'AAA'))]





share|improve this answer

















  • 1




    Surely there is an existing Q&A for this? (I searched but couldn't find one)
    – smci
    Nov 19 at 22:46















up vote
3
down vote










up vote
3
down vote









Assigning a list of columns to a single name can result in the warning message. Instead it can be



DT[, c('n', 'n2', 'n3') := .(str_count(strings, 'A'), 
str_count(strings, 'AA'), str_count(strings, 'AAA'))]





share|improve this answer












Assigning a list of columns to a single name can result in the warning message. Instead it can be



DT[, c('n', 'n2', 'n3') := .(str_count(strings, 'A'), 
str_count(strings, 'AA'), str_count(strings, 'AAA'))]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 22:12









akrun

392k13181253




392k13181253








  • 1




    Surely there is an existing Q&A for this? (I searched but couldn't find one)
    – smci
    Nov 19 at 22:46
















  • 1




    Surely there is an existing Q&A for this? (I searched but couldn't find one)
    – smci
    Nov 19 at 22:46










1




1




Surely there is an existing Q&A for this? (I searched but couldn't find one)
– smci
Nov 19 at 22:46






Surely there is an existing Q&A for this? (I searched but couldn't find one)
– smci
Nov 19 at 22:46




















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383344%2fhow-to-assign-list-as-observation-in-data-table%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen