Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

reshape - From wide form to long form using gather function

Suppose that we have a data set including six variables and id variable like below:

id v2019 v2020 v2021 u2019 u2020 u2021

In this case, if we use Stata we can make this long form very easily using the "reshape" command like below.

reshape long v u, i(id) j(year) => id v u

But, when I use the gather function in tidyr package, this function makes just three columns every time.

data %>% gather(key = "key, value = "value") => id key value

that is gather function gathers "every" columb with out seperate of different variables suchs as u and v.

So my question is that how can I imitate the function of reshape in stata using gather in r.

thank you for your time spent to read this question.

question from:https://stackoverflow.com/questions/66047913/from-wide-form-to-long-form-using-gather-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can you reshape the data using more conventional methods and then use the tidyr package?

A minimal example of your code (and data) would be helpful to understand where goes wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...