1
$\begingroup$

I am reshaping my dataframe as follows.

enter image description here

I want to set the Wavelength column as the column names with the code as follows.

dcast(unique(df1, by=cols), ID + Condition ~ Wavelength, value.var = 'mean')

But all the values became 1s as follows.

enter image description here

I would appreciate any help.

Yanpeng

$\endgroup$
1
  • 1
    $\begingroup$ I think this should be posted on Stack Overflow - you’d get a better answer there anyway. But please make sure to post data as plain text and not as an image, otherwise it’ll likely get closed :) thanks $\endgroup$
    – user438383
    May 20 at 16:47

1 Answer 1

1
$\begingroup$

That comment is right, it's a general programming question, and text would be much easier for people to help with rather than images.

But, since I ran into this not long ago...

Take a look at the message shown in blue just above the output table. My guess is you have some repeated ID/Condition/Wavelength combinations and it's defaulting to reporting the number of matching rows in the long-format data frame. You'll need to either make sure you have all unique combinations, or explicitly define what you expect it to do with multiple values going into a single output row/column position (like, median/mean/max/min/whatever). The unique() call won't do it since varying mean values would still make for unique input rows.

For example compare this:

> reshape2::dcast(data.frame(
+     Wavelength=c(664, 666, 662),
+     Condition="late",
+     ID=c(6221, 6222, 6223),
+     mean=c(106817, 110357, 112998)),
+   ID + Condition ~ Wavelength, value.var = "mean")
    ID Condition    662    664    666
1 6221      late     NA 106817     NA
2 6222      late     NA     NA 110357
3 6223      late 112998     NA     NA

with this:

> reshape2::dcast(data.frame(
+     Wavelength=c(664, 664, 666, 662),
+     Condition="late",
+     ID=c(6221, 6221, 6222, 6223),
+     mean=c(106800, 106817, 110357, 112998)),
+   ID + Condition ~ Wavelength, value.var = "mean")
Aggregation function missing: defaulting to length
    ID Condition 662 664 666
1 6221      late   0   2   0
2 6222      late   0   0   1
3 6223      late   1   0   0
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.