0

I have a dataframe comprising of 300 variables with four observations each. For instance, one of the variables looks like this.

Afghanistan
34
34
56
45

I'm doing an ANOVA so I need the data to look like:

Afghanistan 34
Afghanistan 34
Afghanistan 56
Afghanistan 45

How do I do this for all 300 variables? My reasoning is so that I can use the aov function to run the anova. In this case the country is considered to be a treatment, and with 4 observations from each treatment. Any guidance will be appreciated!

Abby Liu
  • 13
  • 5
  • @divibisan they're trying to go long to wide, not wide to long. Either way, there are plenty of dupes – camille Mar 09 '19 at 00:27

1 Answers1

0

As divibisan's comment suggests, this question is quite similar to other questions on Stack Overflow. You problem is fairly common in data processing projects, what you are trying to accomplish is referred to as "Transforming the data from a wide to long format."

There are many R packages which come with built in functions to accomplish this task, such as reshape() or melt/cast from the reshape2 package. However, if you are uncomfortable using these functions as a "black box" solution here is a way you could manually construct the desired data set.

  ex<-data.frame(USA=1:4, FRANCE=5:8)
  ex

    USA FRANCE
    1      5
    2      6
    3      7
    4      8


  country<-names(ex)
  country_names<-c()
  vals<-c()

  for (i in 1:ncol(ex)){

   country_names<-c(country_names,(rep(country[i],4)))

   vals<-c(vals,ex[,i])

  }

  transformed<-data.frame(country=country_names, value = vals)

  transformed

  country value
      USA     1
      USA     2
      USA     3
      USA     4
   FRANCE     5
   FRANCE     6
   FRANCE     7
   FRANCE     8
M.Bergen
  • 174
  • 10