简体   繁体   English

将重复观察的列变成R中每个观察的列

[英]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!提前致谢!

How my current data look我当前的数据如何

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.

相关问题 R-将每个字母因子的字母数字字符观测值拆分为列,并为每个观测值分配数值 - R - split alphanumeric char observations with column for each letter factor with value of numeric for each observation 在 R 中将特定的观察和行变成列 - Turn Specific Observation and Row into Column in R R - 对于列中的每个观察值,在另一列中找到最接近的观察值 - R - for each observation in a column, find the closest one in another column R:按列组汇总数据-使用每个观察值对列进行变异 - R: Aggregating data by column group - mutate column with values for each observation R 中的行到列观察 - Observation in Row to Column in R R - 应用函数 - 用 0 替换观察值,根据观察值的变量来确定要开始的列 - R - Apply function - Replacing Observations with 0 Depending on an Observation's Variable to Determine which column to Start 如何在 R 中按组在数据帧中迭代地将所有先前的观察结果除以最后一个观察结果,然后存储结果 - How to divide all previous observations by the last observation iteratively within a data frame column by group in R and then store the result 如何知道一列中每个观测的频率并将它们按r排序? - How to know the frequency of each observation in a column and sort them in r? 复制观察 r 中另一列中每一行的列 - replicate observation of column for each row in another columns in r 包括 columnheader 作为 R 中每个观察的另一个列值 - include columnheader as another column value for each observation in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM