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

About the Author

Question Info

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

Related Question

statsBy函数错误,“x”必须是数字

Ruby on Rails -跟踪对第三方服务的API调用

合并左连接的STRING_SPLIT后的多个结果行

将Derby连接到java

如何在python中从字节转换到bytearray ?

Gatsby-image-plugin和contentful

win10开机显示GIGABYTE页面开不了机,如何解决?(操作系统-windows)

change attr element by javascript

检索带order by 非常慢

如何使用ORDER BY DESC会话变量?