Multiple Variables (in columns), multiple Years(in columns) to reshape to flatfile in R

Beginner

I have data in the following format, with Variables, data by years and where A, B, C, D are the row id's.

        Variable 1     blank column       Variable 2
  2008 2009 2010 2011                2008 2009 2010 2011
A   1   5    9    13                   5   10   15   20
B   2   6    10   14                  25   30   35   40
C   3   7    11   15                  45   50   55   60
D   4   8    12   16                  65   70   75   80

I would like to get it in this format:

  Variable   Year  Data
A Variable1  2008  1
A Variable1  2009  5
.....
.....
D Variable2  2010  75
D Variable2  2011  80

I thought of using gather from library(tidyr) but I cant figure out jow to do it. Your help is greatly appreciated. Sorry do not have a reproducible example.

structure(list(X1 = c(NA, "A", "B", "C", "D"), Variable1 = c(2008, 
1, 2, 3, 4), X3 = c(2009, 5, 6, 7, 8), X4 = c(2010, 9, 10, 11, 
12), X5 = c(2011, 13, 14, 15, 16), Variable1 = c(2008, 5, 25, 
45, 65), X7 = c(2009, 10, 30, 50, 70), X8 = c(2010, 15, 35, 55, 
75), X9 = c(2011, 20, 40, 60, 80)), .Names = c("X1", "Variable1", 
"X3", "X4", "X5", "Variable1", "X7", "X8", "X9"), row.names = c(NA, 
5L), class = "data.frame")
Prem

Hope this helps!

library(tidyverse)

names(df) <- c("row_name", 
               paste(c(t(replicate(4, names(df)[1 + seq(1, length.out=floor(length(names(df))/4), by=4)]))),
                     df[1,-1], 
                     sep="_"))

df[-1,] %>%
  gather(Variable_Year, Data, -row_name) %>%
  separate(Variable_Year, into=c("Variable", "Year"), sep="_") %>%
  arrange(row_name, Variable, Year)

Note that you can't have non-unique values as "row names" of a dataframe so you may need to think of an alternative way to handle below row_name column.

Output is:

   row_name  Variable Year Data
1         A Variable1 2008    1
2         A Variable1 2009    5
...
31        D Variable2 2010   75
32        D Variable2 2011   80

Sample data:

df -> structure(list(row_name = c(NA, "A", "B", "C", "D"), Variable1_2008 = c(2008, 
1, 2, 3, 4), Variable1_2009 = c(2009, 5, 6, 7, 8), Variable1_2010 = c(2010, 
9, 10, 11, 12), Variable1_2011 = c(2011, 13, 14, 15, 16), Variable2_2008 = c(2008, 
5, 25, 45, 65), Variable2_2009 = c(2009, 10, 30, 50, 70), Variable2_2010 = c(2010, 
15, 35, 55, 75), Variable2_2011 = c(2011, 20, 40, 60, 80)), .Names = c("row_name", 
"Variable1_2008", "Variable1_2009", "Variable1_2010", "Variable1_2011", 
"Variable2_2008", "Variable2_2009", "Variable2_2010", "Variable2_2011"
), row.names = c(NA, 5L), class = "data.frame")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive