Having trouble with tidyr gather()

Having trouble with tidyr gather()



Hi I am trying to create a long dataframe from Re_NM_df


head(Re_NM_df, n = 3)
AustinPulse.Remain AustinPulse.NM1_SpacePump AustinPulse.NM4_Nothing
1 NA 0 0
2 NA 0 0
3 NA 0 0



However, I get this error


RE_NM_Long <- gather(data = Re_NM_df, "NurseMom", "Likely", AustinPulse$Remain,
AustinPulse$NM1_SpacePump)
Error: `AustinPulse$Remain` must evaluate to column positions or names, not an
integer vector
In addition: Warning message:
'glue::collapse' is deprecated.
Use 'glue_collapse' instead.
See help("Deprecated") and help("glue-deprecated")



Here is my intended output.


NursingMom Remain Available
1 NM1_Space 1 1
2 NM1_Space 4 0
3 NM4_Nothing 2 1*



*Respondent selected that nothing was available.



I just can't seem to code the gather function well. any help is appreciated.



Thank you.




1 Answer
1



Thanks for the question. It would be great to see an example of what you want the final data frame to look like. To assist, below I have created a reproducible example using tidyr::gather to turn your example data into a 'long' data frame.


tidyr::gather



A key thing to remember when using gather is to specify the column names that form the 'key' and 'value' pair. You can also select and exclude columns to gather as an additional argument.


gather


library(tibble)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)

x <- tribble(
~AustinPulse.Remain, ~AustinPulse.NM1_SpacePump, ~AustinPulse.NM4_Nothing,
NA, 0, 0,
NA, 0, 0,
NA, 0, 0
)

# gathering all columns
x %>%
tidyr::gather(attribute, value)
#> # A tibble: 9 x 2
#> attribute value
#> <chr> <dbl>
#> 1 AustinPulse.Remain NA
#> 2 AustinPulse.Remain NA
#> 3 AustinPulse.Remain NA
#> 4 AustinPulse.NM1_SpacePump 0.
#> 5 AustinPulse.NM1_SpacePump 0.
#> 6 AustinPulse.NM1_SpacePump 0.
#> 7 AustinPulse.NM4_Nothing 0.
#> 8 AustinPulse.NM4_Nothing 0.
#> 9 AustinPulse.NM4_Nothing 0.

# here I have excluded AustinPulse.Remain from the gather
x %>%
tidyr::gather(NursingMom, value, -AustinPulse.Remain) %>%
dplyr::select(NursingMom, value, AustinPulse.Remain)
#> # A tibble: 6 x 3
#> NursingMom value AustinPulse.Remain
#> <chr> <dbl> <lgl>
#> 1 AustinPulse.NM1_SpacePump 0. NA
#> 2 AustinPulse.NM1_SpacePump 0. NA
#> 3 AustinPulse.NM1_SpacePump 0. NA
#> 4 AustinPulse.NM4_Nothing 0. NA
#> 5 AustinPulse.NM4_Nothing 0. NA
#> 6 AustinPulse.NM4_Nothing 0. NA





thank you. Let me edit the question to show my intended output. I think your code will get me to where I want to be.
– Luis Pablo Martinez
Aug 30 at 23:45





will the code you shared work for my intended output? thank you for your contribution.
– Luis Pablo Martinez
Aug 31 at 0:01





I'm still a little unclear on your data, particularly the column 'Available' and what type of aggregation you are doing. But I have updated my answer to get you a little closer. Hopefully I have answered your question on how to use the gather function. If so please accept.
– Dean
Aug 31 at 0:06






thank you! that is exactly what I wanted. Maybe my problem stemmed from not being clear on what I wanted the Available to represent. By the way, where did you pick up your tidyr skills?
– Luis Pablo Martinez
Aug 31 at 0:20





You're welcome. Lots of good resources out there, check out the tidyverse website and an excellent online book R for Data Science. Good luck
– Dean
Aug 31 at 0:29



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

Node.js puppeteer - Use values from array in a loop to cycle through pages