I am looking to read a cvs file that contains a matrix where each row/column combination has a specific rate. See example:
I would like to read the information as a data frame, where I have each value associated with a row/column factor with $Year
,$Age
,$Rate
. See example:
Thanks!
CS
Update: Thanks for the feedback, I've written my own function and seems to do the trick:
matrix2dataframe <- function(rowN,colN,Data) {
NRowRep = max(rowN)-min(rowN)
NColRep = max(colN)-min(colN)
tmp_Name = rep(as.character(Data[rowN[1],colN[1]]),times=NRowRep*NColRep)
tmp_Value = as.numeric(as.matrix(Data[rowN[2]:max(rowN),colN[2]:max(colN)]))
tmp_Age = rep(as.character(as.matrix(Data[rowN[2]:max(rowN),1])),times=NColRep)
tmp_DYear = mapply(rep,x=as.character(as.matrix(Data[rowN[1],2:max(colN)])),times=NRowRep)
tmp_DYear = as.character(tmp_DYear)
#list(Cancer = tmp_Name,Rate = tmp_Value,Age = tmp_Age,DYear = tmp_DYear)
data.frame(Cancer = tmp_Name,Rate = tmp_Value,Age = tmp_Age,DYear = tmp_DYear)
}