使用列名称中的id变量重新整形R [重复](Reshape with id-variable in column names R [duplicate])
这个问题在这里已有答案:
- 收集多组列[重复] 5个答案
我已经和R一起工作了很长一段时间,但很少使用reshape或reshape2包。 我目前正在尝试从宽到长格式重塑数据集,其中指标变量是变量名称的一部分。 这是我数据框的当前结构:
mydf <- data.frame(district = c(1:2), v.mandate = c(1, 3), s.mandate = c(2, 4), v.perc = c(.4, .3), s.perc = c(.5, .6)) > mydf district v.mandate s.mandate v.perc s.perc 1 1 1 2 0.4 0.5 2 2 3 4 0.3 0.6
我想将其重新整理为长格式并提取“v”。 和“s。” 作为id变量(在实际数据集中,列表更长)。 见下面的例子。
mydf2 <- data.frame(district = c(1, 1, 2, 2), party = c("v", "s", "v", "s"), mandate = c(1, 2, 3, 4), perc = c(.4, .5, .3, .6)) > mydf2 district party mandate perc 1 1 v 1 0.4 2 1 s 2 0.5 3 2 v 3 0.3 4 2 s 4 0.6
我尝试过使用reshape和melt函数,但我似乎无法从变量名中提取指标变量。 而是将数据集重新整形为长格式,但将完整的变量名称作为id变量。 见下面的例子。
> melt(mydf, id.vars=1) district variable value 1 1 v.mandate 1.0 2 2 v.mandate 3.0 3 1 s.mandate 2.0 4 2 s.mandate 4.0 5 1 v.perc 0.4 6 2 v.perc 0.3 7 1 s.perc 0.5 8 2 s.perc 0.6
这可能是一个微不足道的问题,但我无法在线找到解决方案。
非常感谢任何帮助!
This question already has an answer here:
I've worked with R for quite some time now, but have made little use of the reshape or reshape2 packages. I'm currently trying to reshape a data set from wide to long format where the indicator variables are part of the variable names. This is the current structure of my data frame:
mydf <- data.frame(district = c(1:2), v.mandate = c(1, 3), s.mandate = c(2, 4), v.perc = c(.4, .3), s.perc = c(.5, .6)) > mydf district v.mandate s.mandate v.perc s.perc 1 1 1 2 0.4 0.5 2 2 3 4 0.3 0.6
I want to reshape this to long format and extract the "v." and "s." as id variables (in the real data set the list is longer). See example below.
mydf2 <- data.frame(district = c(1, 1, 2, 2), party = c("v", "s", "v", "s"), mandate = c(1, 2, 3, 4), perc = c(.4, .5, .3, .6)) > mydf2 district party mandate perc 1 1 v 1 0.4 2 1 s 2 0.5 3 2 v 3 0.3 4 2 s 4 0.6
I have tried using both the reshape and melt functions, but I can't seem to extract the indicator variables from the variable names. Instead, the data set is reshaped into long format, but with the full variable names as id variables. See example below.
> melt(mydf, id.vars=1) district variable value 1 1 v.mandate 1.0 2 2 v.mandate 3.0 3 1 s.mandate 2.0 4 2 s.mandate 4.0 5 1 v.perc 0.4 6 2 v.perc 0.3 7 1 s.perc 0.5 8 2 s.perc 0.6
It might be a trivial problem, but I haven't been able to find a solution on-line.
Greatly appreciate any help!
原文:https://stackoverflow.com/questions/29077443
满意答案
j = jet; j(1,:) = [ 1 1 1 ]; colormap(j); imagesc(Img);
唯一的问题是图像中映射到最低颜色的任何其他东西也会变成白色。
j = jet; j(1,:) = [ 1 1 1 ]; colormap(j); imagesc(Img);
Only thing is that anything else in the image that maps to the lowest color is going to also come out white.
相关问答
更多Matlab - 自动设置imagesc中的比例(Matlab - setting up the scale in imagesc automatically)
在MATLAB中告诉imagesc使用特定的数字(Tell imagesc in MATLAB to use specific figure)
使用滑块实时更新imagesc的matlab(Matlab realtime updating of imagesc with a slider)
MATLAB:将imagesc()RESULT转换为图像(MATLAB: Convert imagesc() RESULT to an image)
MATLAB:imagesc()和image()以不同的方式显示相同的颜色表(MATLAB: imagesc() and image() display the same colormap differently)
使用imwrite写出imagesc输出的真实颜色(Use imwrite to write true color of the output of imagesc)
imagesc在matlab中绘制到矩阵(imagesc plot to matrix in matlab)
matlab的imagesc背景颜色(matlab's imagesc background color)
Matlab,如何获得imagesc生成的结果?(Matlab, How to get the result generated by imagesc?)
更改imagesc图中像素的颜色,而不重新绘制Matlab / Octave中的所有内容(Change the color of a pixel in an imagesc plot without repainting everything in Matlab/Octave)
相关文章
更多nutch与起点R3集成之笔记(四)
nutch与起点R3集成之笔记(一)
nutch与起点R3集成之笔记(二)
R语言实战视频教程-尚学堂视频教程
R简单数据分析
在Hadoop集群上运行R程序--安装RHadoop
Apache Solr 实现去掉重复的搜索结果
请问起点R3能否做到完全支持MultiCore?
nutch与起点R3集成之笔记(三)
利用SOLR搭建企业搜索平台 之八(solr 实现去掉重复的搜索结果,打SOLR-236_collapsing.patch补丁)
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!