Answers.Body: <p>That comment is right, it's a general programming question, and text would be much easier for people to help with rather than images.</p>
  <p>But, since I ran into this not long ago...</p>
  <p>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.</p>
  <p>For example compare this:</p>
  <pre><code>&gt; reshape2::dcast(data.frame(
  +     Wavelength=c(664, 666, 662),
  +     Condition=&quot;late&quot;,
  +     ID=c(6221, 6222, 6223),
  +     mean=c(106817, 110357, 112998)),
  +   ID + Condition ~ Wavelength, value.var = &quot;mean&quot;)
      ID Condition    662    664    666
  1 6221      late     NA 106817     NA
  2 6222      late     NA     NA 110357
  3 6223      late 112998     NA     NA
  </code></pre>
  <p>with this:</p>
  <pre><code>&gt; reshape2::dcast(data.frame(
  +     Wavelength=c(664, 664, 666, 662),
  +     Condition=&quot;late&quot;,
  +     ID=c(6221, 6221, 6222, 6223),
  +     mean=c(106800, 106817, 110357, 112998)),
  +   ID + Condition ~ Wavelength, value.var = &quot;mean&quot;)
  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
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-20T17:28:45.040Z
Answers.LastActivityDate: 2022-05-20T17:28:45.040Z
Answers.UserDisplayName: Jesse
Body: <p>I am reshaping my dataframe as follows.</p>
  <p><a href="https://i.sstatic.net/cOU0g.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cOU0g.png" alt="enter image description here" /></a></p>
  <p>I want to set the <code>Wavelength column</code> as the column names with the code as follows.</p>
  <pre><code>dcast(unique(df1, by=cols), ID + Condition ~ Wavelength, value.var = 'mean')
  </code></pre>
  <p>But all the values <code>became 1s</code> as follows.</p>
  <p><a href="https://i.sstatic.net/yDRCI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/yDRCI.png" alt="enter image description here" /></a></p>
  <p>I would appreciate any help.</p>
  <p>Yanpeng</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-20T16:47:57.453Z
Comments.Text: 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
Comments.UserDisplayName: user438383
Tags: <r>
Title: Why all values become 1 after dcast?
ThreadText: # Why all values become 1 after dcast?
  
  (Asked by: username_0 on 2022-05-20)
  
  I am reshaping my dataframe as follows.
  
  [![enter image description here](https://i.sstatic.net/cOU0g.png)](https://i.sstatic.net/cOU0g.png)
  
  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](https://i.sstatic.net/yDRCI.png)](https://i.sstatic.net/yDRCI.png)
  
  I would appreciate any help.
  
  Yanpeng
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: 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  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-05-20. Score: 1
  
  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
  
  ```
  
  
  ---
  
  