- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 dcast.data.table 将长数据表转换为宽数据表
library(data.table)
library(reshape2)
set.seed(1234)
dt.base <- data.table(A = rep(c(1:3),2), B = rep(c(1:2),3), C=c(1:4,1,2),thevalue=rnorm(6))
#from long to wide using dcast.data.table()
dt.cast <- dcast.data.table(dt.base, A ~ B + C, value.var = "thevalue", fun = sum)
#now some stuff happens e.g., please do not bother what happens between dcast and melt
setkey(dt.cast, A)
dt.cast[2, c(2,3,4):=1,with = FALSE]
现在我想将 data.table 再次融化回原来的列布局,我被卡住了,我如何将连接的列名称与转换的 data.table 分开,这是我的问题
dt.melt <- melt(dt.cast,id.vars = c("A"), value.name = "thevalue")
我需要两列而不是一列
我正在寻找的结果可以用这段代码产生
#update
dt.base[A==2 & B == 1 & C == 1, thevalue :=1]
dt.base[A==2 & B == 2 & C == 2, thevalue :=1]
#insert (2,1,3 was not there in the base data.table)
dt.newrow <- data.table(A=2, B=1, C=3, thevalue = 1)
dt.base <-rbindlist(list(dt.base, dt.newrow))
dt.base
一如既往地感谢任何帮助
最佳答案
这对你有用吗?
colnames <- c("B", "C")
dt.melt[, (colnames) := (colsplit(variable, "_", colnames))][, variable := NULL]
subset(dt.melt, thevalue != 0)
# or dt.melt[thevalue != 0, ]
# A thevalue B C
#1: 1 -1.2070657 1 1
#2: 2 1.0000000 1 1
#3: 2 1.0000000 1 3
#4: 3 1.0844412 1 3
#5: 2 1.0000000 2 2
#6: 3 0.5060559 2 2
#7: 1 -2.3456977 2 4
如果您的数据集不可表示,并且有效行中可能有零,这里是替代方法
colnames <- c("B", "C")
setkey(dt.melt[, (colnames) := (colsplit(variable, "_",colnames))][, variable := NULL], A, B, C)
setkey(dt.base, A, B, C)
dt.base <- dt.melt[rbind(dt.base, data.table(A = 2, B = 1, C = 3), fill = T)]
dt.base[, thevalue.1 := NULL]
## A B C thevalue
## 1: 1 1 1 -1.2070657
## 2: 1 2 4 -2.3456977
## 3: 2 1 1 1.0000000
## 4: 2 2 2 1.0000000
## 5: 3 1 3 1.0844412
## 6: 3 2 2 0.5060559
## 7: 2 1 3 1.0000000
编辑
作为。 @Arun 建议,最有效的方法是使用@AnandaMahto cSplit 函数,因为它也在使用 data.table
,即,
cSplit(dt.melt, "variable", "_")
第二次编辑
为了节省手动合并,您可以在 dcasting 时设置 fill = NA
(例如),然后使用 csplit
一次性完成所有操作,例如
dt.cast <- dcast.data.table(dt.base, A ~ B + C, value.var = "thevalue", fun = sum, fill = NA)
setkey(dt.cast, A)
dt.cast[2, c(2,3,4):=1,with = FALSE]
dt.melt <- melt(dt.cast,id.vars = c("A"), value.name = "thevalue")
dt.cast <- cSplit(dt.melt, "variable", "_")[!is.na(thevalue)]
setnames(dt.cast, 3:4, c("B","C"))
# A thevalue B C
# 1: 1 -1.2070657 1 1
# 2: 2 1.0000000 1 1
# 3: 2 1.0000000 1 3
# 4: 3 1.0844412 1 3
# 5: 2 1.0000000 2 2
# 6: 3 0.5060559 2 2
# 7: 1 -2.3456977 2 4
关于r - 我如何融化具有连接列名称的 data.table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25066831/
我有一个复杂的 melt 函数需要执行。我试过逐步解决它并检查其他问题,但我很困惑! 这是我当前的数据框: 1 2 4 5 6 10 24 Userid u_
我知道,这个问题已经被问过好几次了,但我没有设法根据已经问过的问题构建我的解决方案。 DF 我有: id| country | series name | 2015 | 2016 | 2017
我正在尝试从名为 df 的数据帧创建以下名为 out 的数据帧。我有一种非常手动且缓慢的方法,但我希望它可以通过 groupby() 和 melt() 的组合来完成 import pandas as
我有一个像这样的 pandas DataFrame: df = pd.DataFrame({'custid':[1,2,3,4], ...: 'prod1':['jeans','tshirt','ja
我有这样一个数据框 NSW VIC 0 6718023 5023203 1 6735528 5048207 2 6742690 5061266 3 6766133 50
给定一个如下形式的方形 pandas DataFrame: a b c a 1 .5 .3 b .5 1 .4 c .3 .4 1 upper triangle 怎么可能?被熔化得到如
我玩 pandas 是为了适应它,我问自己是否可以在 pandas 中使用 Melt 功能而不需要太多麻烦? 我正在使用相当著名的titanic.csv数据集。 titanic = pd.read_c
我有一个关于航类的数据集,我有兴趣找出乘坐同一航类的一对乘客坐在同一排。假设所有航类中只有 Row 是唯一的,这意味着如果第 1 行在 10 号航类中被占用,那么它就不能出现在 11 号航类中。 即。
考虑这个 Pandas 数据框: df = pd.DataFrame({ 'User ID': [1, 2, 2, 3], 'Cupcakes': [1, 5, 4, 2],
这是我导入的数据框的前几行的示例(在完整数据集中,主题变量共有五个级别/因子,另外两个是代数 II 和几何)。 SID firstName lastName subject su
我有以下数据框: df = pd.DataFrame({'Date':['01/01/2021','08/01/2021'], 'a_score':[7,3],
我是一名优秀的程序员,十分优秀!