Collapse a data.frame into a vector
If your data.frame
is named dat
, try:
unlist(dat, use.names=FALSE)
You may also want to try the following:
as.character(unlist(dat, use.names=FALSE))
c(na.omit(as.character(unlist(dat, use.names=FALSE))))
which might be useful if your data are entered as factors.
Also, in your question, you make it appear that you don't actually have a basic "vector", but rather a one-column data.frame
. Thus, something like dat2 <- data.frame(COL = c(na.omit(as.character(unlist(dat, use.names=FALSE)))))
might be what you're looking for.
R: Collapse values in data.frame into a vector similar to paste(collapse=',')
Make cyls
a list column like this:
my_df <- mtcars %>% group_by(gear) %>% summarize(cyls=list(unique(cyl)))
my_df$cyls[[1]] # boom dbl vectors for each row stored as a list
[1] 6 8 4
Collapse every series of four rows in a data frame into a single vector, overwriting missing values
How about this:
library(dplyr)
library(tidyr)
df <- df %>% mutate(obs = rep(1:(nrow(.)/4), each=4))
df <- df %>%
pivot_longer(-obs, names_to="var", values_to="vals") %>%
na.omit() %>%
group_by(obs) %>%
mutate(col = seq_along(obs)) %>%
select(obs, col, vals) %>%
pivot_wider(names_from="col", names_prefix="V", values_from="vals")
df
# # A tibble: 3 x 7
# # Groups: obs [3]
# obs V1 V2 V3 V4 V5 V6
# <int> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 1 Buy Completed 2021-02-11 20:49:19 0.11057 Fee1.00 USD Total199.00 USD
# 2 2 Buy Completed 2021-02-11 20:48:03 82.146 Fee0.50 USD Total100.00 USD
# 3 3 Buy Completed 2021-02-11 20:47:22 30.15 Fee0.64 USD Total127.00 USD
Collapse cells across several rows to a single column in a R data frame
Try this base R
approach:
#Code
data <- aggregate(x3~x1+x2,data,function(x) paste0(x,collapse = ','))
Output:
x1 x2 x3
1 1 A 6,7
2 3 C 8,9
3 5 E 10
R dataframe - Collapse multiple columns into a single numeric vector, row-by-row
I don't think davo1979 is looking to transform his data to a long format, therefore, I don't think tidyr::gather is the tool he wants.
The answer, which I think, most closestly address his question is
lapply(1:nrow(mtcars), function(x) as.numeric(mtcars[x,2:8]))
This will store each row as a numeric vector, contained within a list.
However, davo1979 it is important to know what is your ultimate goal with this data. I ask because your desired outcome of a bunch of numeric vectors is a clumsy way to handle and store data in R.
Aggregate and collapse a vector based while maintaing order
Using DF
shown reproducibly in the Note at the end, sort by from
giving DF2
and then iterate through its rows removing any row with a duplicate. We need a loop here since each removal depends on the prior ones. Finally summarize the result.
library(dplyr)
DF2 <- arrange(DF, from)
i <- 1
while(i <= nrow(DF2)) {
ix <- seq_len(i-1)
dup <- with(DF2, (to[i] %in% c(to[ix], from[ix])) | (from[i] %in% to[ix]))
if (dup) DF2 <- DF2[-i, ] else i <- i + 1
}
DF2 %>%
group_by(from) %>%
summarize(to = toString(to), priority = sum(priority)) %>%
ungroup
giving:
# A tibble: 4 x 3
from to priority
<int> <chr> <int>
1 1 8, 7 6
2 2 6 1
3 3 4 1
4 9 5 3
Note
Lines <- "from | to | priority
1 | 8 | 1
2 | 6 | 1
3 | 4 | 1
4 | 5 | 3
5 | 6 | 4
6 | 2 | 5
7 | 8 | 2
4 | 3 | 5
2 | 1 | 1
6 | 6 | 4
1 | 7 | 5
8 | 4 | 6
9 | 5 | 3"
DF <- read.table(text = Lines, header = TRUE, sep = "|", strip.white = TRUE)
collapse a dataframe in R that contains both numeric and character variables
Is this what you are looking for?
library(dplyr)
data %>%
dplyr::group_by(ag, date) %>%
summarise(across(everything(), ~
if(is.numeric(.x)) mean(.x) else first(.x)))
#> `summarise()` has grouped output by 'ag'. You can override using the `.groups` argument.
#> # A tibble: 12 x 6
#> # Groups: ag [4]
#> ag date num_var1 num_var2 alpha_var1 alpha_var2
#> <chr> <int> <dbl> <dbl> <chr> <chr>
#> 1 A 1 3 22 A Y
#> 2 A 2 11 14 I Q
#> 3 A 3 19 6 Q I
#> 4 B 1 4 21 B X
#> 5 B 2 12 13 J P
#> 6 B 3 20 5 R H
#> 7 C 1 5 20 C W
#> 8 C 2 13 12 K O
#> 9 C 3 21 4 S G
#> 10 D 1 6 19 D V
#> 11 D 2 14 11 L N
#> 12 D 3 22 3 T F
Created on 2022-03-03 by the reprex package (v2.0.1)
Merging data frame columns into a vector
We can have a list
column. If we specify the simplify = FALSE
in combn
, it would return a list
of vectors, which can be wrapped in tibble
to create a list
column
combn(5, 2, simplify = FALSE) %>%
tibble(vector = .)
Or another option is to make use of unnest_wider
with unite
library(tidyr)
library(dplyr)
library(stringr)
combn(5, 2, simplify = FALSE) %>%
tibble(vector = ., vector2 = vector) %>%
unnest_wider(c(vector2), names_repair =
~ c('vector', str_c('V', 1:2))) %>%
unite(string, V1, V2, sep=";", remove = FALSE)
If we use the OP's method, an option is map2
which returns a list
after concatenate both the columns elementwise
library(purrr)
combn(5, 2) %>%
t %>%
as.data.frame %>%
mutate(
string = paste(V1, V2, sep = ';'),
vector = map2(V1, V2, c))
# V1 V2 string vector
#1 1 2 1;2 1, 2
#2 1 3 1;3 1, 3
#3 1 4 1;4 1, 4
#4 1 5 1;5 1, 5
#5 2 3 2;3 2, 3
#6 2 4 2;4 2, 4
#7 2 5 2;5 2, 5
#8 3 4 3;4 3, 4
#9 3 5 3;5 3, 5
#10 4 5 4;5 4, 5
Related Topics
Using Shorthand Character Classes Inside Character Classes in R Regex
How to Move the Bibliography in Markdown/Pandoc
Read.Table Reads "T" as True and "F" as False, How to Avoid
Generating Non-Duplicate Combination Pairs in R
Suppress Automatic Output to Console in R
Preventing Column-Class Inference in Fread()
Why Doesn't Comparison Between Numeric and Character Variables Give a Warning
Ggplot Set Scale_Color_Gradientn Manually
Str_Replace (Package Stringr) Cannot Replace Brackets in R
Random Sampling to Give an Exact Sum
Remove Rows Which Have All Nas in Certain Columns
A Vector to an Upper Triangle Matrix by Row in R
Positioning Shiny Widgets Beside Their Headers
How to Use the Spread Function Properly in Tidyr
Extract Columns from Data Table by Numeric Indices Stored in a Vector