Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:24:30+00:00 2026-05-31T10:24:30+00:00

I have a data.frame that looks like this. x a 1 x b 2

  • 0

I have a data.frame that looks like this.

x a 1 
x b 2 
x c 3 
y a 3 
y b 3 
y c 2 

I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like:

    a    b    c
x   1    2    3
y   3    3    2

I have tried cast from the reshape package and I have tried writing a manual function to do this but I do not seem to be able to get it right.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:24:31+00:00Added an answer on May 31, 2026 at 10:24 am

    There are many ways to do this. This answer starts with what is quickly becoming the standard method, but also includes older methods and various other methods from answers to similar questions scattered around this site.

    tmp <- data.frame(x=gl(2,3, labels=letters[24:25]),
                      y=gl(3,1,6, labels=letters[1:3]), 
                      z=c(1,2,3,3,3,2))
    

    Using the tidyverse:

    The new cool new way to do this is with pivot_wider from tidyr 1.0.0. It returns a data frame, which is probably what most readers of this answer will want. For a heatmap, though, you would need to convert this to a true matrix.

    library(tidyr)
    pivot_wider(tmp, names_from = y, values_from = z)
    ## # A tibble: 2 x 4
    ## x         a     b     c
    ## <fct> <dbl> <dbl> <dbl>
    ## 1 x       1     2     3
    ## 2 y       3     3     2
    

    The old cool new way to do this is with spread from tidyr. It similarly returns a data frame.

    library(tidyr)
    spread(tmp, y, z)
    ##   x a b c
    ## 1 x 1 2 3
    ## 2 y 3 3 2
    

    Using reshape2:

    One of the first steps toward the tidyverse was the reshape2 package.

    To get a matrix use acast:

    library(reshape2)
    acast(tmp, x~y, value.var="z")
    ##   a b c
    ## x 1 2 3
    ## y 3 3 2
    

    Or to get a data frame, use dcast, as here: Reshape data for values in one column.

    dcast(tmp, x~y, value.var="z")
    ##   x a b c
    ## 1 x 1 2 3
    ## 2 y 3 3 2
    

    Using plyr:

    In between reshape2 and the tidyverse came plyr, with the daply function, as shown here: https://stackoverflow.com/a/7020101/210673

    library(plyr)
    daply(tmp, .(x, y), function(x) x$z)
    ##    y
    ## x   a b c
    ##   x 1 2 3
    ##   y 3 3 2
    

    Using matrix indexing:

    This is kinda old school but is a nice demonstration of matrix indexing, which can be really useful in certain situations.

    with(tmp, {
      out <- matrix(nrow=nlevels(x), ncol=nlevels(y),
                    dimnames=list(levels(x), levels(y)))
      out[cbind(x, y)] <- z
      out
    })
    

    Using xtabs:

    xtabs(z~x+y, data=tmp)
    

    Using a sparse matrix:

    There’s also sparseMatrix within the Matrix package, as seen here: R – convert BIG table into matrix by column names

    with(tmp, sparseMatrix(i = as.numeric(x), j=as.numeric(y), x=z,
                           dimnames=list(levels(x), levels(y))))
    ## 2 x 3 sparse Matrix of class "dgCMatrix"
    ##   a b c
    ## x 1 2 3
    ## y 3 3 2
    

    Using reshape:

    You can also use the base R function reshape, as suggested here: Convert table into matrix by column names, though you have to do a little manipulation afterwards to remove an extra columns and get the names right (not shown).

    reshape(tmp, idvar="x", timevar="y", direction="wide")
    ##   x z.a z.b z.c
    ## 1 x   1   2   3
    ## 4 y   3   3   2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a dataset that looks like this: a <- data.frame(rep(1,5),1:5,1:5) b <- data.frame(rep(2,5),1:5,1:5)
I have a data.frame that looks like this: 320 Dutch A7 3 321 Dutch
I have the following data frame (info) that looks like this: > info[1:5,] field
I have a data.frame named pp that looks like this: > head(pp) X02R X03N
Let's say I have some data in R that looks like this: c(0.11, NA,
I have a editable FormPanel that looks like this: I now want to display
I have data that looks like this: > head(data) date price volume 1 2011-06-26
I have data frame that looks like the following models cores time 1 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.