- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想绘制两种类型的值(dupl 和 orig)。是否可以轻松 reshape 以下数据框
record=c("r1","r1","r2","r3","r3")
v1=rep(0,5)
v2=c(0,0,1,0,0)
v3=c(1,1,0,1,1)
type=c("orig","dupl","orig","orig","dupl")
df<-data.frame(record, v1, v2, v3, type)
df
record v1 v2 v3 type
1 r1 0 0 1 orig
2 r1 0 0 1 dupl
3 r2 0 1 0 orig
4 r3 0 0 1 orig
5 r3 0 0 1 dupl
看起来像这样?
record v1.orig v2.orig v3.orig v1.dupl v2.dupl v3.dupl
r1 0 0 1 0 0 1
r2 0 1 0
r3 0 0 0 0 0 0
关键是我可以绘制 vX.orig 与 vX.dupl 的对比图。或者有更好的方法吗?我正在查看 dcast() 但似乎无法得到我想要的,可能是因为我的数据只是部分熔化(沿着类型?)。
编辑:这是我尝试过的:
df1<-melt(df,id="record")
dcast(df1,record~value, margins=TRUE)
最佳答案
你可以这样做:
library(reshape2)
melted <- melt(df, id.vars= c("record", "type"))
dcast(melted, record ~ variable + type)
record v1_dupl v1_orig v2_dupl v2_orig v3_dupl v3_orig
1 r1 0 0 0 0 1 1
2 r2 NA 0 NA 1 NA 0
3 r3 0 0 0 0 1 1
或者我原来的回答:
library(tidyverse)
df %>% gather(vx, num, -record, -type) %>%
unite(type, vx, type) %>%
spread(type, num)
关于r - 在 R 中,使用 melt 和 cast reshape "mixed"数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42078558/
我有一个这样的DataFrame >>> df = pd.DataFrame([[1,1,2,3,4,5,6],[2,7,8,9,10,11,12]], c
很抱歉,如果这是一个重复的问题,但我找不到问题的答案。 我有一个当前的数据框: 深度 Year Counts Depth.1 Year.1 Counts.1 Depth.2 Year.2 Counts
我正在尝试更改数据集的结构 目前有: RE id Country 0 1 2 ... n 1001 CN,TH CN TH nan ...
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: Error: could not find function … in R 我找到了对函数 melt() 的各种引用
这个问题已经有答案了: How to reshape data from long to wide format (14 个回答) 已关闭 7 年前。 我有像这样的巨大数据框: SN = c(1:10
我有一个小问题,在概念上似乎很简单,但我找不到解决方法... 假设我有一个 data.frame df2,其中有一列列出汽车品牌,另一列包含每个品牌的所有型号,用“,”分隔。我已经获得了 df2 聚合
我想融合 dataframe 以便在一列中我有日期,然后我有用户名作为变量,最后是值。 我收到此错误: Error in as.Date.numeric(value) : 'origin' must
我有几个不同长度的视频剪辑。我想在这些剪辑上混音。 我用这个: melt original.mp4 -audio-track preview.mp3 -transition mix 如果 previe
我目前正在进行一项测试,其中我有不同的区域和一些相关的统计数据,以及位于这些区域的以逗号分隔的基因列表。此列表的数量是可变的,并且可能不包含任何内容 ("NA")。 我怎样才能“融化”这个数据框:
我有一个像这样的宽格式数据集 Index Country Variable 2000 2001 2002 2003 2004 2005 0 Argentina var1
我只想在 pandas 中使用 melt 函数,但我总是遇到同样的错误。 只需键入文档提供的示例: cheese = pd.DataFrame({'first' : ['John', 'Mary'],
在pandas标签上,我经常看到用户询问关于熊猫融化的问题。我将尝试一个规范的Q&A(自我回答)与这个主题。。我要澄清的是:。我看到了一些关于融化的更热门的问题,比如:。使用Pandas将列转换为行:
我有一个如下所示的数据框: Country Person A B C 0 USA Bob 200 300 400 1 Canada Alice 100
例如这是我的数据框 df = pd.DataFrame([['Bob', 'lunch', 70], ['Bob', 'dinner', 160],
我有下面的 SQL 表,我们称之为teams_score,它显示了一场比赛的 2 名球员的球队及其得分。每次他们以 Player1 或 Player2 出现在团队中时,我都需要观察每个球员的得分。我认
我有一个如下所示的数据框: 名称 值 1 值 2 A 100 101 A 100 102 A 100 103 B 200 201 B 200 202 B 200 203 C 300 301 C 30
假设我有以下 data.frame: foo foo CONTACT_DATE ISSUE ISSUE_COUNT RANK 1 2015-09-15 abc 1000
例如,我有以下 map : {'df1': Jan Feb Mar 1 3 5 2 4 6 '
问题 我发现自己在使用 reshape2::melt获得单个“长”data.frame来自分层 list的 data.frame对象。但是,结果的列名称具有标记为“L1”、“L2”等的列表层次结构级别
我正在尝试融化我的 Pandas 数据框,但我不确定如何正确分配变量。我查看了堆栈上的其他示例,但似乎找不到与此匹配的变体。我的数据框 (df1) 如下所示: [IN]: df1 [OUT]:
我是一名优秀的程序员,十分优秀!