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 7191669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:46:42+00:00 2026-05-28T19:46:42+00:00

I have the following data – it is a dump from a normalized database,

  • 0

I have the following data – it is a dump from a normalized database, but I can not access the database, and the database maintainer insists that this is not necessary.

The obs variable is the unique observation id, a.k.a. the one to “pivot” around

Specifically, I want to go from this olddata to the newdata data frame below:

> olddata
    species obs variable value
3      ADFA   1     mean     4
4      ADFA   1      lat   118
5      ADFA   1      lon    49
6      ADFA   1     masl    74
96     HODO   8     mean    18
97     HODO   8      lat   120
98     HODO   8      lon    45
99     HODO   8     masl    36
189    HODO   9     mean    34
190    HODO   9      lat   126
191    HODO   9      lon    12
192    HODO   9     masl    35

I would like to reshape this data frame to look like:

> newdata
  species obs mean lat lon masl
1    ADFA   1    4 118  49   74
2    HODO   8   18 120  45   36
3    HODO   9   34 126  12   35

Disclaimer: this has likely been asked before but I am unable to find the question among the many questions related to transforming data frames / matrices


Here are the dataframes for use when reproducing this issue:

olddata <- structure(list(species = c("ADFA", "ADFA", "ADFA", "ADFA", "HODO", 
             "HODO", "HODO", "HODO", "HODO", "HODO", "HODO", "HODO"), obs = c(1, 
             1, 1, 1, 8, 8, 8, 8, 9, 9, 9, 9), variable = c("mean", "lat", 
             "lon", "masl", "mean", "lat", "lon", "masl", "mean", "lat", "lon", 
             "masl"), value = c(4, 118, 49, 74, 18, 120, 45, 36, 34, 126, 
             12, 35)), .Names = c("species", "obs", "variable", "value"), 
             row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
              "11", "12"), class = "data.frame")

newdata <- structure(list(species = c("ADFA", "HODO", "HODO"), obs = c(1, 
             8, 9), mean = c(4, 18, 34), lat = c(118, 120, 126), lon = c(49, 
             45, 12), masl = c(74, 36, 35)), .Names = c("species", "obs", 
             "mean", "lat", "lon", "masl"), row.names = c(NA, -3L), 
             class = "data.frame")
  • 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-28T19:46:42+00:00Added an answer on May 28, 2026 at 7:46 pm

    Here is an example:

    > library(reshape2)
    > dcast(olddata, species+obs~variable)
      species obs lat lon masl mean
    1    ADFA   1 118  49   74    4
    2    HODO   8 120  45   36   18
    3    HODO   9 126  12   35   34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data in a database table, Columns : Date, Hour(from 1
I have the following data that I parsed from an xml file and now
I have following data to save in database using php my data is :
I have the following data in my database (comma separated strings): word, test, hello
I have the following data set that I am trying to plot with ggplot2,
I have the following data: a b c d FROM:<uniquepattern1> e f g h
I have the following data structure in my database: LastName FirstName CourseName John Day
I have the following data that basically I only need a few bits of
I have the following data that looks like this for example: 34 foo 34
I have the following data that really should be one JSON string however I

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.