gpt4 book ai didi

r - 为什么 reshape2 为我融化返回值 = NA?

转载 作者:行者123 更新时间:2023-12-05 00:32:45 25 4
gpt4 key购买 nike

为什么要 reshape2 melt返回 value = NA为了我?

它适用于 reshape,但不适用于 reshape2:

这是一个示例数据文件:

"","station_id","year","month","day","h1","h2","h3","h4","h5","h6","h7","h8","h9","h10","h11","h12","h13","h14","h15","h16","h17","h18","h19","h20","h21","h22","h23","h24"
"1",1,2004,1,1,46,46,45,41,39,35,33,33,36,47,53,54,55,55,55,55,52,46,40,40,39,38,40,41
"2",1,2004,1,2,43,44,46,46,47,47,47,47,47,47,47,49,52,56,54,56,57,53,50,47,46,45,45,45
"3",1,2004,1,3,45,46,46,44,43,46,46,47,51,55,56,59,65,68,69,68,68,65,64,63,62,63,63,62
"4",1,2004,1,4,63,62,62,62,60,60,60,62,60,64,64,66,71,70,71,72,71,68,67,67,65,64,65,64
"5",1,2004,1,5,64,63,65,64,64,64,64,64,65,66,66,67,68,68,66,66,66,66,63,54,52,49,47,47
"6",1,2004,1,6,47,46,45,43,41,41,39,39,40,43,45,44,45,46,46,46,45,39,39,39,38,36,32,32

假设它保存为 /tmp/foo.csv , 然后:

使用 reshape :
$ R
...
Type 'q()' to quit R.

> library("reshape")
Loading required package: plyr

Attaching package: ‘reshape’

The following object(s) are masked from ‘package:plyr’:

rename, round_any

> hlist <- NULL; for(z in 1:24) { hlist <- cbind(hlist, sprintf("h%d",z)) }
>
> thh <- read.csv('/tmp/foo.csv')
> thm <- melt(thh,measure.vars=hlist,variable="hour")
> head(thm)
station_id year month day hour value
1 1 2004 1 1 h1 46
2 1 2004 1 2 h1 43
3 1 2004 1 3 h1 45
4 1 2004 1 4 h1 63
5 1 2004 1 5 h1 64
6 1 2004 1 6 h1 47
> q()

使用 reshape2:
$ R
...
Type 'q()' to quit R.

> library("reshape2")
> hlist <- NULL; for(z in 1:24) { hlist <- cbind(hlist, sprintf("h%d",z)) }
>
> thh <- read.csv('/tmp/foo.csv')
> thm <- melt(thh,measure.vars=hlist,variable="hour")
> head(thm)
station_id year month day hour value
1 1 2004 1 1 h1 NA
2 1 2004 1 2 h1 NA
3 1 2004 1 3 h1 NA
4 1 2004 1 4 h1 NA
5 1 2004 1 5 h1 NA
6 1 2004 1 6 h1 NA
> q()

你可以看到使用 library("reshape") , value列有数字,但对于 libary("reshape2") ,它有 NA ,对于相同的数据。

最佳答案

有更好的方法来做你想做的事情。

以下所有内容都适用于 melt()来自 reshape2 :

# Not using hlist
melt(th, measure.vars=5:ncol(th), variable="hour")
melt(th, id.vars=1:4, variable="hour")

# Using your hlist
hlist <- NULL; for(z in 1:24) { hlist <- cbind(hlist, sprintf("h%d",z)) }
melt(th, measure.vars=as.vector(hlist), variable="hour")

# Using an alternative hlist
hlist <- paste0("h", 1:24)
melt(th, measure.vars=hlist, variable="hour")

好像 melt() from "reshape"接受一个矩阵作为 measure.vars 的输入,但是 melt()来自“reshape2”没有(我觉得这更合理)。

更新:可重现问题示例

仅供引用,以下是一种从头到尾的方式,您可以通过方便其他 Stack Overflow 用户复制和粘贴的方式来分享此问题:
# Use set.seed when you want to use random numbers 
# but want others to have the same data as you.
set.seed(1)

# Make up some data that mimics your actual dataset
# Does not have to be your exact dataset
th <- cbind(
data.frame(station = rep(LETTERS[1:3], each = 3),
year = 2004, month = rep(1:3, times = 3)),
setNames(data.frame(
matrix(sample(100, 45, replace = TRUE), nrow = 9)),
paste0("h", 1:5)))

hlist <- NULL; for(z in 1:5) { hlist <- cbind(hlist, sprintf("h%d",z)) }
# Cleanup any unnecessary stuff that your code leaves behind in the workspace
rm(z)

现在,证明你的问题。您可以使用 detach(package:package_name)而不必退出并重新启动 R。
library(reshape)
head(melt(th, measure.vars = hlist, variable = "hour"))
# station year month hour value
# 1 A 2004 1 h1 27
# 2 A 2004 2 h1 38
# 3 A 2004 3 h1 58
# 4 B 2004 1 h1 91
# 5 B 2004 2 h1 21
# 6 B 2004 3 h1 90
detach(package:reshape)

library(reshape2)
head(melt(th, measure.vars = hlist, variable = "hour"))
# station year month hour value
# 1 A 2004 1 h1 <NA>
# 2 A 2004 2 h1 <NA>
# 3 A 2004 3 h1 <NA>
# 4 B 2004 1 h1 <NA>
# 5 B 2004 2 h1 <NA>
# 6 B 2004 3 h1 <NA>
detach(package:reshape2)

希望这可以帮助!

关于r - 为什么 reshape2 为我融化返回值 = NA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990398/

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