gpt4 book ai didi

r - 为长数据库进行宽变换,在 R 中对变量进行分组

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:12 24 4
gpt4 key购买 nike

<分区>

我想修改我的数据库。我想做的是将我的数据库的每一行都转换为一个人。

我希望它看起来像这样。示例:

ID    massaseca Fator   Anexo     Teor  Tempo
1 334,68 AM c 0 0
1 344,19 AM c 0 10
1 347,32 AM c 0 20
1 350,2 AM c 0 30
1 352,52 AM c 0 40
. . . . . .
. . . . . .

虽然在论坛上找了一些例子,但还是无法解决我的问题。我将介绍这两种尝试。当我执行下面的代码时,我发现它覆盖了一些变量并且不是你想要的方式。

####################################
require(reshape2)
long <- melt(dados1, id.vars = c("Teor", "Fator"))
melt(dados1, id.vars = 1:2)
melt(dados1, measure.vars = 4:45)
melt(dados1, measure.vars = as.character(10,20,30,
40,50,60,70,80,
90,105,120,135,150,
170,190,210,230,250,
270,290,310,330,350,
360,405,450,510,570,
630,690,750,810,870,
930,990,1050,1110,1170,
1230,1290,1350))

################# CASE 2 ###########
####################################
library(data.table)
long1 <- melt(setDT(dados1), id.vars = c("Teor", "Fator"), variable.name = "Tempo")
long1
melt(setDT(dados1), id.vars = 1:2, variable.name = "Tempo")
melt(setDT(dados1), measure.vars = 4:45, variable.name = "Tempo")
melt(setDT(dados1), measure.vars = as.character(10,20,30,
40,50,60,70,80,
90,105,120,135,150,
170,190,210,230,250,
270,290,310,330,350,
360,405,450,510,570,
630,690,750,810,870,
930,990,1050,1110,1170,
1230,1290,1350), variable.name = "Tempo")


但是,上面的例子是将一个变量填充到另一个变量下面,怎么办?

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