Reputation: 23
I am trying to store two numerical arrays in an unusal form, e.g. c('[1;2;3]','[4;5;6]','[24;25;26]')
I need them in a CSV in single cells, like here
So far I tried this:
DF
ID time Y
1 3 23
1 4 24
1 5 20
2 2 12
2 8 15
3 2 19
3 3 23
3 5 21
3 6 32
timeList = list()
yList = list()
for (i in 1:3) {
timeList[i] = DF$time
yList[i] = DF$Y
}
longTimeList = list(timeList)
longYList = list(yList)
DF <- data.frame(ID = c(1,2,3),
P1 = longTimeList,
P2 = longYList)
so the example DF woul read
newDF <- data.frame(ID = c(1,2,3),
P1 = c('[3;4;5]','[2;8]','[2;3;5;6]'),
P2 = c('[23,24,20]','[12;15]','[19;23;21;32]'))
which should be stored in a CSV file to give something like that shown above.
Upvotes: 2
Views: 51
Reputation: 887981
An option with glue
library(dplyr)
library(glue)
library(stringr)
df %>%
group_by(ID) %>%
summarise(across(everything(), function(x)
glue("[{str_c(x, collapse = ';')}]")))
# A tibble: 3 x 3
# ID time Y
# <int> <glue> <glue>
#1 1 [3;4;5] [23;24;20]
#2 2 [2;8] [12;15]
#3 3 [2;3;5;6] [19;23;21;32]
Upvotes: 1
Reputation: 8880
using tidyverse
df <-
structure(list(
ID = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L),
time = c(3L,
4L, 5L, 2L, 8L, 2L, 3L, 5L, 6L),
Y = c(23L, 24L, 20L, 12L, 15L,
19L, 23L, 21L, 32L)
),
class = "data.frame",
row.names = c(NA,
-9L))
library(tidyverse)
df %>%
group_by(ID) %>%
summarise(across(everything(), ~paste0(.x, collapse = ";"))) %>%
mutate(across(c(time, Y), ~paste0("[", .x, "]")))
#> # A tibble: 3 x 3
#> ID time Y
#> <int> <chr> <chr>
#> 1 1 [3;4;5] [23;24;20]
#> 2 2 [2;8] [12;15]
#> 3 3 [2;3;5;6] [19;23;21;32]
Created on 2021-06-08 by the reprex package (v2.0.0)
Upvotes: 2
Reputation: 102920
I believe aggregate
could help you
> aggregate(. ~ ID, DF, function(x) sprintf("[%s]", paste0(x, collapse = ";")))
ID time Y
1 1 [3;4;5] [23;24;20]
2 2 [2;8] [12;15]
3 3 [2;3;5;6] [19;23;21;32]
Upvotes: 4