-4

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)
}
Chen Shen
  • 1
  • 1
  • Please give sample data and/or an image of the data. – www Sep 03 '17 at 01:49
  • 1
    You can give the output of `dput` for the two example data.frames instead of two pictures. See here for how to make a good question https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – mt1022 Sep 03 '17 at 01:54
  • this doesnt really differ from your other question https://stackoverflow.com/questions/46019182/importing-matrix-csv-data-into-r-how-to-convert-into-dataframe . Did you have a look at any of the introductory guides which will cover this - if so can you show what code you tried and where you are stuck. Thanks – user20650 Sep 03 '17 at 02:32
  • Thanks for the link. I wasn't able to find any default function so I ended up writting my own – Chen Shen Sep 03 '17 at 02:44

2 Answers2

2

Simply Using reshape

 melt(df)

    age  variable value
1 15-99  one_year  76.3
2 15-44  one_year  92.9
3 15-99 five_year  60.9
4 15-44 five_year  82.8
BENY
  • 317,841
  • 20
  • 164
  • 234
0

Have a look at this:

df <- data.frame(age = c("15-99", "15-44"), "one_year" = c(76.3, 92.9), "five_year" = c(60.9, 82.8))
df
    age one_year five_year
1 15-99     76.3      60.9
2 15-44     92.9      82.8

library(tidyr)
gather(df, year, rate, -age)
    age      year  rate
1 15-99  one_year  76.3
2 15-44  one_year  92.9
3 15-99 five_year  60.9
4 15-44 five_year  82.8

Should you indeed have a matrix to start with, you can convert it to a data frame with the data.frame() function. Have a look at ?data.frame. Should the age-groups be rownames, consider using df$age = row.names(df) to convert the row names to a column.

coffeinjunky
  • 11,254
  • 39
  • 57