[英]how to convert character values in rows into columns in R
I want to use a for loop to compute cumulative abnormal returns in R and have the dates in column 1, company names (comn) in 2, and stock prices (prccd) in 3. However, to compute abnormal returns I first need to compute the returns and thus need the company names with subsequent stock prices to be able to do this.我想使用 for 循环来计算 R 中的累积异常收益,并在第 1 列中包含日期,在 2 中包含公司名称 (comn),在 3 中包含股票价格 (prccd)。但是,要计算异常收益,我首先需要计算回报,因此需要公司名称和随后的股价才能做到这一点。 Now my df looks like this;
现在我的 df 看起来像这样;
Date![]() |
conm![]() |
prccd ![]() |
---|---|---|
2016-11-02 ![]() |
X ![]() |
1 ![]() |
2016-11-02 ![]() |
Y![]() |
2 ![]() |
2016-11-02 ![]() |
Z ![]() |
3 ![]() |
And thus need the data frame to look like:因此需要数据框看起来像:
Date![]() |
X ![]() |
Y![]() |
Z ![]() |
---|---|---|---|
2016-11-02 ![]() |
1 ![]() |
2 ![]() |
3 ![]() |
2016-11-03 ![]() |
2 ![]() |
3 ![]() |
4 ![]() |
2016-11-04 ![]() |
3 ![]() |
4 ![]() |
5 ![]() |
Maybe you want to transform your dataframe, dcast
function in data.table would help.也许你想改变你的 dataframe,
dcast
function in data.table 会有所帮助。 You can see here ( how to transform rows to columns in R ) for usage您可以在此处查看( 如何将 R 中的行转换为列)以供使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.