首页 \ 问答 \ 使用列名称中的id变量重新整形R [重复](Reshape with id-variable in column names R [duplicate])

使用列名称中的id变量重新整形R [重复](Reshape with id-variable in column names R [duplicate])

这个问题在这里已有答案:

我已经和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
更新时间:2022-03-18 07:03

最满意答案

 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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)