使用列名称中的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.
相关问答
更多-
您可以使用caxis设置色阶的限制 img1 = randn(100); img2 = rand(100); %# find global min/max clim(1) = min(min(img1(:)),min(img2(:))); clim(2) = max(max(img1(:)),max(img2(:))); figure ah1 = axes; imagesc(img1) caxis(ah1,clim) %# show the other figure set the same limi ...
-
imagesc采用'parent'参数。 这是通过image功能间接记录的。 例如: imdata = imread('ngc6543a.jpg'); f = figure; a1 = subplot(211); a2 = subplot(212); image(imdata,'parent',a1); imagesc(imdata,'parent',a2); imagesc takes a 'parent' parameter. This is indirectly documented via the ...
-
滑块可能具有非整数值。 在将其用作索引之前,您需要对其进行舍入 updateSystem(h, squeeze(T(round(es.Value), :, :))) 您可以使用以下内容作为回调来更新图像数据,而不是尝试使用updateSystem (控制系统工具箱功能)。 b.Callback = @(es, ed)set(h, 'CData', squeeze(T(round(es.Value), :, :))); It is possible for the slider to have a non ...
-
%turn off the axes axis off %save the image saveas(h,'test.png') %read the saved image im_fft = imread('test.png'); %remove white border sum_img = sum(im_fft,3); sum_img(sum_img(:) ~= 255*3) = 0; sum_img = logical(sum_img); im_fft = im_fft(~all(sum_img,2), ...
-
MATLAB:imagesc()和image()以不同的方式显示相同的颜色表(MATLAB: imagesc() and image() display the same colormap differently)[2023-10-21]
imagesc缩放颜色轴,而image不缩放。 这就是为什么颜色看起来不同。 如果你点击彩条按钮,你会看到它们在不同的色阶上。 您可以使用caxis更改caxis 。 顺便说一下,如果您只想缩放X轴和Y轴,则可以使用任一功能。 只需提供您自己的缩放x和y矢量。 imagesc scales the color axis, whereas image does not. That's why the colors look different. If you click the colorbar button ... -
这里是Cris Luengo方法的替代方法,它不依赖于imagesc生成的实际情节。 % load a test image I = rgb2gray(imread('peppers.png')); % recreate image cmap = colormap(); % grab current colormap ncolors = size(cmap,1); % do what imagesc does Iind = double(I) - double(min(I(:))); I ...
-
imagesc的数据存储在CData属性中,因此使用它来提取它: X = get(h, 'CData'); 假设h是你的手柄。 现在X包含您的图像数据,您可以将其保存到文件中,例如: save somefile.mat X The data for imagesc is stored in the CData property, so use this to extract it: X = get(h, 'CData'); assuming h is your handle. Now X contai ...
-
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 wh ...
-
要以与imagesc相同的方式缩放图像,请执行以下操作 Amin = min(A(:)); Amax = max(A(:)); A_scaled = (A - Amin)/(Amax - Amin); 为了证明缩放的图像是图像在内部执行的操作,请尝试此操作 imagesc(A,[Amin Amax]); pause imagesc(A_scaled); To scale the image in the same way as imagesc do the following Amin = min(A( ...
-
您可以使用图像的CData属性来处理每个像素中的数据,而无需一遍又一遍地关闭和重绘图形,轴或轴对象。 最终比尝试生成新的图像对象或只是反复调用imagesc()快1.5倍 - 2倍。 在这些循环中,不要忘记drawnow调用,否则MATLAB将尝试跳过图形绘制,直到循环完成。 示例代码: data = rand(200, 200); % Data to display figure(1) % Make a figure imgHand = imagesc(data); % Display data in ...