简体   繁体   中英

Convert 2 column r data to heatmap

In RI have a dataframe with 2 columns, x and y locations, which I need to alter:

df = data.frame(x = c(1,3,2,6,2), y = c(2,1,5,3,5))

My actual data frame has thousands of values. I first wish to add a third column to show the number if incidences where particular x and y values occur. For example, if rows with x = 2 and y = 5 are found twice then we remove one of these rows and place a 2 in the third column of the row.

I then need to alter this 3 column data frame to a matrix where the third column value is the value in the array at row (x) and column (y).

Ultimately this is to produce a series of heatmaps, these particular formats seem the best best testing out a few packages. I have made some attempts myself but have had no luck.

This can be achieved by using data.table as follows:

df = data.frame(x = c(1,3,2,6,2), y = c(2,1,5,3,5))

library(data.table)
setDT(df)[
  # count unique combinations of x-y-values
  , .N, by = .(x, y)][
    # fill missing to complete heatmap using cross join
    CJ(x = 1:max(x), y = 1:max(y)), on = .(x, y)][
      # replace NA
      is.na(N), N := 0][
        # reshape from long to wide
        , dcast(.SD, x ~ y)][
          # coerce to matrix
            , as.matrix(.SD), .SDcols = -"x"]
  1 2 3 4 5 [1,] 0 1 0 0 0 [2,] 0 0 0 0 2 [3,] 1 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 [6,] 0 0 1 0 0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM