I have a data frame with the following structure:
d1 <- data.frame(
stringsAsFactors = FALSE,
Stock = c("Stock 1","Stock 1","Stock 1",
"Stock 2","Stock 2","Stock 2","Stock 3","Stock 3",
"Stock 3"),
Column.1 = c("...","...","...","...",
"...","...","...","...","..."),
Column.2 = c("...","...","...","...",
"...","...","...","...","..."),
Value = c(1, 1, 1, 0.5, 0.5, 0.5, 0.2, 0.2, 0.2)
)
| Stock | Column 1 | Column 2 | Value |
|---|---|---|---|
| Stock 1 | ... | ... | 1 |
| Stock 1 | ... | ... | 1 |
| Stock 1 | ... | ... | 1 |
| Stock 2 | ... | ... | 0.5 |
| Stock 2 | ... | ... | 0.5 |
| Stock 2 | ... | ... | 0.5 |
| Stock 3 | ... | ... | 0.2 |
| Stock 3 | ... | ... | 0.2 |
| Stock 3 | ... | ... | 0.2 |
But with 2000 stocks. Basically each stock has the same value in the given column. I would like to extract this value for each stock and put it in another data frame which is a list of stocks with different variables that looks like this:
d2 <- data.frame(
stringsAsFactors = FALSE,
Stock = c("Stock 1", "Stock 2", "Stock 3", "Stock 4", "Stock 5"),
Variable.1 = c("y", "y", "y", "y", "y"),
Variable.2 = c("x", "x", "x", "x", "x"),
Value = c("1", "0.5", "0.2", "...", "...")
)
| Stock | Variable 1 | Variable 2 | Value |
|---|---|---|---|
| Stock 1 | y | x | 1 |
| Stock 2 | y | x | 0.5 |
| Stock 3 | y | x | 0.2 |
| Stock 4 | y | x | ... |
| Stock 5 | y | x | ... |
How do I do this? :)
Edit: There are other columns than the one I want to extract the data from. :)