gpt4 book ai didi

r - 将广泛的数据收集/融合到不同的值列中

转载 作者:行者123 更新时间:2023-12-03 19:44:27 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Gather multiple sets of columns

(5 个回答)


5年前关闭。




我有宽格式的数据,它们有两组不同的值列:包含质量(Mass1、Mass2 等)和包含相应日期(Mass1_date、Mass2_date 等)的那些。

library(tidyr)
library(dplyr)
library(lubridate)

df <- structure(list(Year = 2004, Nest_no = 21, Mass1 = 2325, Mass1_date = structure(1081987200, class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Mass2 = 2000, Mass2_date = structure(1082851200, class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Mass3 = 1750, Mass3_date = structure(1083715200, class = c("POSIXct",
"POSIXt"), tzone = "UTC")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .Names = c("Year", "Nest_no", "Mass1",
"Mass1_date", "Mass2", "Mass2_date", "Mass3", "Mass3_date"))

df

## Source: local data frame [1 x 8]
##
## Year Nest_no Mass1 Mass1_date Mass2 Mass2_date Mass3 Mass3_date
## (dbl) (dbl) (dbl) (time) (dbl) (time) (dbl) (time)
## 1 2004 21 2325 2004-04-15 2000 2004-04-25 1750 2004-05-05

我想将数据“整理”成长格式,其中两组值列是 gather ed ( melt ed) 转换为两个不同的值列,一列包含“质量列”的值,另一列包含“日期列”的值:
## Source: local data frame [3 x 5]
##
## Year Nest_no capture date weight
## (dbl) (dbl) (dbl) (date) (dbl)
## 1 2004 21 1 2004-04-15 2325
## 2 2004 21 2 2004-04-25 2000
## 3 2004 21 3 2004-05-05 1750

起初,我以为我可以用 tidyr并分两步完成。
gather(df, capture, date, contains("Date")) %>% 
gather(capture2, weight, contains("Mass"))

## Source: local data frame [9 x 6]
##
## Year Nest_no capture date capture2 weight
## (dbl) (dbl) (chr) (time) (chr) (dbl)
## 1 2004 21 Mass1_date 2004-04-15 Mass1 2325
## 2 2004 21 Mass2_date 2004-04-25 Mass1 2325
## 3 2004 21 Mass3_date 2004-05-05 Mass1 2325
## 4 2004 21 Mass1_date 2004-04-15 Mass2 2000
## 5 2004 21 Mass2_date 2004-04-25 Mass2 2000
## 6 2004 21 Mass3_date 2004-05-05 Mass2 2000
## 7 2004 21 Mass1_date 2004-04-15 Mass3 1750
## 8 2004 21 Mass2_date 2004-04-25 Mass3 1750
## 9 2004 21 Mass3_date 2004-05-05 Mass3 1750

但是,它没有按预期工作。经过几次尝试,我上来了
使用此解决方案:
df <- gather(df, capture2, weight, contains("Mass"), convert = T) %>% 
mutate(capture = extract_numeric(capture2))

## Warning: attributes are not identical across measure variables; they will
## be dropped

df$capture2 <- ifelse(grepl("date", df$capture2), "date", "weight")

df <- spread(df, capture2, weight) %>%
mutate(date = as.Date(as.POSIXct(date, origin = "1970-01-01")))

df

## Source: local data frame [3 x 5]
##
## Year Nest_no capture date weight
## (dbl) (dbl) (dbl) (date) (dbl)
## 1 2004 21 1 2004-04-15 2325
## 2 2004 21 2 2004-04-25 2000
## 3 2004 21 3 2004-05-05 1750

我想知道是否有更好的方法来实现这一目标?

谢谢你,菲利普

最佳答案

我们可以通过 melt 轻松做到这一点来自 data.table . measure可以多拍patterns列名并将“宽”格式转换为“长”格式。

library(data.table)
melt(as.data.table(df), measure=patterns('\\d$', 'date$'),
variable.name='capture', value.name= c('weight', 'date'))
# Year Nest_no capture weight date
#1: 2004 21 1 2325 2004-04-15
#2: 2004 21 2 2000 2004-04-25
#3: 2004 21 3 1750 2004-05-05

关于r - 将广泛的数据收集/融合到不同的值列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35218669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com