如何解决在 tidyverse 中每行整理多个观察
我想要整理每行有多个观察值的数据。
数据是一组卫星传感器的相对光谱响应 (RSR)。我做了一个玩具数据集,代表这里的actual data。每个传感器/波段都有两列,一列是测试的波长范围(“Wvln(nm)”),另一列是该传感器的响应(“RSR”)。在我的玩具数据中,波段 1 对 500 nm 有很强的响应,波段 2 对 600 nm 有很强的响应。
library(tidyverse)
rsr_toy <- tibble::tribble(
~`Band 1`,~`...2`,~`Band 2`,~`...4`,"Wvln(nm)","RSR","500","0.9","0.01","600","0.12","0.8"
)
# remove the first row containing metadata
rsr1 <- rsr_toy %>%
slice(-1) %>%
janitor::clean_names("small_camel") %>%
rsr1
# # A tibble: 2 x 4
# band1 x2 band2 x4
# <chr> <chr> <chr> <chr>
# 1 500 0.9 500 0.01
# 2 600 0.12 600 0.8
我想整理数据,所以每个观察都有自己的行,如下所示:
# desired outcome:
tibble::tribble(
~sensor,~wavelength,~rsr,"band1",500,0.9,600,0.12,"band2",0.01,0.8
)
# # A tibble: 4 x 3
# sensor wavelength rsr
# <chr> <dbl> <dbl>
# 1 band1 500 0.9
# 2 band1 600 0.12
# 3 band2 500 0.01
# 4 band2 600 0.8
如何使用 tidyr 简单地完成此操作?
解决方法
获取长格式数据,重命名列并创建sensor
列。
library(tidyverse)
rsr1 %>%
pivot_longer(cols = everything(),names_to = '.value',names_pattern = '(.*?)\\d') %>%
rename(wavelength = band,rsr = x) %>%
mutate(sensor = rep(str_subset(names(rsr1),'band'),length.out = n()),.before = 1)
# sensor wavelength rsr
# <chr> <chr> <chr>
#1 band1 500 0.9
#2 band2 500 0.01
#3 band1 600 0.12
#4 band2 600 0.8
,
我将 tbl 拆分为每个列表的一个传感器,然后在每个传感器上执行一些基本的 dplyr 命令,然后重新加入列表。
- 按波段拆分,以便每个波段都有自己的表(遵循 this 解决方案)。
splits <- seq(2,ncol(rsr1),2) %>%
map(~ select(rsr1,(.-1):all_of(.)))
splits
# [[1]]
# # A tibble: 2 x 2
# band1 x2
# <chr> <chr>
# 1 500 0.9
# 2 600 0.12
#
# [[2]]
# # A tibble: 2 x 2
# band2 x4
# <chr> <chr>
# 1 500 0.01
# 2 600 0.8
- 然后对列表中的每个 tibble 应用自定义函数:
my_rename_tbl <- function(tbl){
tbl %>%
# add a column with the band name
add_column(sensor = colnames(tbl)[1],.before = 1) %>%
# rename the other two columns "wavelength" and "rsr" respectively
rename("wvln" = 2,"rsr" = 3)
}
splits %>%
map(my_rename_tbl) %>%
bind_rows()
# # A tibble: 4 x 3
# sensor wvln rsr
# <chr> <chr> <chr>
# 1 band1 500 0.9
# 2 band1 600 0.12
# 3 band2 500 0.01
# 4 band2 600 0.8
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。