I have a dataframe including the values for each cell (possibly more than one value) and the row and the column index.
df = data.frame(values = c(1,"Sven", 20,"Mueller","sept",2,30,"John","Mar","Hynes","Marc"),
colI = c(1,2,3,2,4,1,3,2,4,2,2), rowI = c(1,1,1,1,1,2,2,2,2,2,2))
And I want to get something similar to the following data.frame:
df_final= data.frame(Index = c(1,2), name = c("Sven, Mueller", "John, Hynes, Marc"), age = c(20,30),
month = c("sept","Mar"))
However, I haven't been able to do anything and I have also not found a solution online. I am not able to find a solution to bring the values to their corresponding place in the dataframe and I think that it is even a bigger challenge that the cells can contain a different amount of values.
Thanks for your help.
You could use
dplyr
,tidyr
andstringr
, all included intidyverse
:returns
Note: I changed your input data a little bit and added a single
2
to yourrowI
column (see Maurits Evers' comment).