0
Follow
0
View

# R tidyverse split strings by commas and calculate mean

xiong_sir 注册会员
2023-01-25 10:05

This is a possibile approach:

``````df %>%
mutate(Time = factor(Time, levels = Time)) %>%
separate(Measurements, sep = ",", into = letters[seq(1, 10)]) %>%
pivot_longer(a:j) %>%
na.omit() %>%
mutate(value = as.numeric(value)) %>%
group_by(Time) %>%
summarise(mean = mean(value))
``````
``````# A tibble: 22 × 2
Time     mean

1 June 7   65.3
2 June 8   63.7
3 June 9   40.7
4 June 10  64.2
5 June 11  64.7
6 June 12  40.5
7 June 13  93.7
8 June 14  49.7
9 June 15  39
10 June 16  43
# … with 12 more rows
``````
deathyeah 注册会员
2023-01-25 10:05

A hacky solution...

``````df <- str_split(df\$Measurements, ', ')
means <- NULL
row <- NULL
for (i in seq_along(df)){
row <- as.numeric(str_split(df[[i]], ', '))
means[i] <- mean(row)
}``````

d510423336 注册会员
2023-01-25 10:05

I think you are looking for something like this:

``````library(tidyverse)
library(stringr)

# to pass data to lapply your way needs '{}'
# use unnamed function \(x) = shorthand for function(x)
df\$Measurements <- df %>%
{lapply(str_split(.\$Measurements, ', '), \(x) x %>%
as.numeric() %>%
mean())} %>%
do.call(rbind, .)

# A tibble: 22 × 2
Time    Measurements[,1]

1 June 7              65.3
2 June 8              63.7
3 June 9              40.7
4 June 10             64.2
5 June 11             64.7
6 June 12             40.5
7 June 13             93.7
8 June 14             49.7
9 June 15             39
10 June 16             43
# … with 12 more rows
``````

dutuoyu 注册会员

Publish Time
2023-01-25 10:05
Update Time
2023-01-25 10:05