Issue reagrding 'longToWide' function in 'lsr' package

I am trying to convert a long format data to wide format data using 'longToWide' funtion in 'lsr' package
I have 29 subjects.
When I run 'longToWide' funtion for the entire 29 subjects, for some subjects, it gives N/A, though there is no missing data in the long format data.

For those subjects, it shows N/A , if run separately, it works properly. But when the subset of subjects put back into whole set, it shows again N/A for some subjects on running the 'longToWide' function.

A typical reason is when there is a "hidden" missing value. That's when, in the long format, there is no apparent missing value, but there is a particular combination of variable that doesn't appear. Then when converting to wide format, the value for that combination is not known.

Modifying the example in ?longToWide:

long <- data.frame(
  id = c(1, 2, 1, 2, 3, 1, 2, 3),
  time = c("t1", "t1", "t2", "t2", "t2", "t3", "t3", "t3"),
  accuracy = c(.50, .03, .94, .63, .49, .78, .71, .16)
)

long
#>   id time accuracy
#> 1  1   t1     0.50
#> 2  2   t1     0.03
#> 3  1   t2     0.94
#> 4  2   t2     0.63
#> 5  3   t2     0.49
#> 6  1   t3     0.78
#> 7  2   t3     0.71
#> 8  3   t3     0.16
lsr::longToWide(long, accuracy ~ time)
#>   id accuracy_t1 accuracy_t2 accuracy_t3
#> 1  1        0.50        0.94        0.78
#> 2  2        0.03        0.63        0.71
#> 3  3          NA        0.49        0.16

Created on 2023-09-21 with reprex v2.0.2

In this case you see a missing value in wide but not long format, that's because the long format does not contain any value for time == t1 and id == 3.

If this is not what happens in your case, you will need to share some example data to reproduce your problem, as detailed here.

Thank you for the quick response..
I understand the hidden missing value.
But in my case, I don't think there are any hidden missing value.

You may look at the data in the link below.

The code I used is here,
dataFrame_wide=longToWide(dataFrame,Lt_Ocpt+Rt_Ocpt~Band+day+condnname+markersegment)

Thanks..

There are problems with the case: "meditation1" and "Meditation1" are different from R's point of view, so some subjects have only "meditation1" and missing "Meditation1" or reverse. Same for "mediatation2"/"Meditation2". There is also some "wm"/"WM". Also, only some subjects have a "wm1" in the condname column (maybe a human mistake when writing down data in the wrong column).

So all these create a lot of missing data, you need to do some input cleaning before you can work with that.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.