How to group a vector into a list of vectors?
split.data.frame
is a good way to organize this; then extract the color component.
d <- data.frame(dressId=c(6,9,10,10,10,12,12),
color=factor(c("yellow","red","green",
"purple","yellow",
"purple","red"),
levels=c("red","orange","yellow",
"green","blue","purple")))
I think the version you want is actually this:
ss <- split.data.frame(d,d$dressId)
You can get something more like the list you requested by extracting the color component:
lapply(ss,"[[","color")
Function to group elements of a vector/list
split
can separate a vector into lists (among other things) based on factors. See ?split
for more information. I became interested in grouping vector of words based on anagrams. So here's one solution for that
vec = c("stop", "pots", "leaves")
split(vec, sapply(vec, function(x)
paste(sort(unlist(strsplit(tolower(gsub(" ", "", x)), ""))), collapse = "")))
#$aeelsv
#[1] "leaves"
#$opst
#[1] "stop" "pots"
How can I add a vector to a list of vectors in R?
You should use list
when with [
e[4] <- list(c(5, 3))
since [
store data in terms of list
in your case.
Or just [[
for assignment
e[[4]] <- c(5,3)
You can type ?[
and read for more information.
R turn each element in a vector into a list of lists
you could use
lapply(vector,list)
Edit: for new desired output
sapply(vector,list)
Split a data.frame by group into a list of vectors rather than a list of data.frames
Using base R
only with split
. It should be faster than the ==
with unique
with(df, split(id, group))
Or with tidyverse
we can pull
the column after the group_split
. The group_split
returns a data.frame/tibble and could be slower compared to the split
only method above. But, here, we can make some performance improvements by removing the group column (keep = FALSE
) and then in the list
, pull
the 'id' column to create the list
of vector
s
library(dplyr)
library(purrr)
df %>%
group_split(group, keep = FALSE) %>%
map(~ .x %>%
pull(id))
Or use {}
with pipe
df %>%
{split(.$id, .$group)}
Or wrap with with
df %>%
with(., split(id, group))
Split column into vectors by group R - independent of column order
You may make df$b
a named vector using setNames
, and then split
it into a list:
split(setNames(df$b, df$a), df$id)
# $id1
# a b c d e
# 1 2 3 4 5
#
# $id2
# a b c d e
# 5 4 3 2 1
How to append vectors to a list group by group
You may use combn()
. Loop over number of elements 0:3
sublist should contain (or 1:3
if empty element not wanted). Except the order this is equivalent to Python [[], [1], [1, 2], [1, 2, 10], [1, 10], [2], [2, 10], [10]]
.
lapply(0:3, \(m) combn(c(1, 2, 10), m, simplify=FALSE)) |>
unlist(recursive=FALSE)
# [[1]]
# numeric(0)
#
# [[2]]
# [1] 1
#
# [[3]]
# [1] 2
#
# [[4]]
# [1] 10
#
# [[5]]
# [1] 1 2
#
# [[6]]
# [1] 1 10
#
# [[7]]
# [1] 2 10
#
# [[8]]
# [1] 1 2 10
Note: R >= 4.1 used.
How do I split a vector into a list of vectors when a condition is met?
The following line seems to work just fine:
split(x,cumsum(x==0))
Related Topics
Get a List of the Data Sets in a Particular Package
Catching an Error and Then Branching Logic
Show Correlations as an Ordered List, Not as a Large Matrix
R: What Are Operators Like %In% Called and How to Learn About Them
Merging More Than 2 Dataframes in R by Rownames
How to Deal with Hdf5 Files in R
Undefined Columns Selected When Subsetting Data Frame
R: Removing Null Elements from a List
Converting Data Frame Column from Character to Numeric
Scale and Size of Plot in Rstudio Shiny
Photo Alignment with Graph in R
Reset the Graphical Parameters Back to Default Values Without Use of Dev.Off()
How to Resolve the "No Font Name" Issue When Importing Fonts into R Using Extrafont
Set the Order of a Stacked Bar Chart by the Value of One of the Variables