[英]Turn a column with repeating observations into a column for each observation in R
My data currently looks like in the photo below.我的数据目前如下图所示。 What I would like is a single column for all unique values of the "permno" variable with the following values of "ret_excess" as rows.
我想要的是“permno”变量的所有唯一值的单个列,其中“ret_excess”的以下值作为行。 So there would be a "10145" column, a "10516" and so on.
所以会有一个“10145”列,一个“10516”等等。 The Permno variable is a stock identifier with ret_excess being monthly returns.
Permno 变量是一个股票标识符,其中 ret_excess 是月回报率。 There is a date variable that says the year/month of the observation, which I have removed but can add if needed:)
有一个日期变量表示观察的年/月,我已将其删除但可以根据需要添加:)
Thanks in advance!提前致谢!
It sounds like you want to pivot the table from long to wide.听起来你想要 pivot 表格从长到宽。 For that to make sense there need to be one or more columns that uniquely identifies each observation.
为了使这一点有意义,需要一个或多个列来唯一标识每个观察结果。
Assuming the date column is a unique identifier for each permno + ret_excess pair, you can do:假设日期列是每个 permno + ret_excess 对的唯一标识符,您可以执行以下操作:
library(tidyr)
df %>%
pivot_wider(id_cols = date, names_from = permno, values_from = ret_excess)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.