Rで何かをしたり、読書をするブログ

政府統計の総合窓口のデータや、OECDやUCIやのデータを使って、Rの練習をしています。ときどき、読書記録も載せています。

IEA Gender and Energy Employment Data Analysis 2 - Organize the data frame with R.

www.crosshyou.info

This post is continuation of the above one.

In the previous post, I load IEA data into R and create a data frame.

In this session, let’s examine and organize the contents of this dataframe.

Let's start with "Country".

I see some countries have 864 observations and some don't.

I would like to use countries which have 864 observations.

Next, let's see Year.

There are three Year, 2010, 2014 and 2018. Each Year has 4320 observations.

Next, let's see sector.

There are 6 sector and each sector has 2160 observation.

Some sectors have too long name. So I will modify them.

Next, let's see Indicator

Some Indicator has less than 810 obs. So, I remove those Indicators.

Just as I renamed the sector levels, I will also rename the indicator levels.

Next, let's see category

We have 9 categories and each one has 1080 observations. I will re-name categories.

All right, I renamed factor variables. Let's see how summary() function returns.

Oops! I forgot to remove n.

Now, I have very consice data frame. 
Today, I finish this post.

Next post is

 

www.crosshyou.info

 


To read from the 1st post of this series,

www.crosshyou.info

Below are R code for this post.

#
# Country
df |> 
  count(Country) |> 
  arrange(desc(n))
#
df |> 
  count(Country) |> 
  arrange(n)
#
# Filter countries which have 864 observations only
df <- df |> 
  group_by(Country) |> 
  mutate(n = n()) |> 
  filter(n == 864) |> 
  select(-n) |> 
  ungroup()
df
#
# Year
df |> 
  count(Year) |> 
  arrange(desc(n))
#
# sector
df |> 
  count(sector) |> 
  arrange(n)
#
# rename factor levels
levels(df$sector) # confirm factor levels
original_levles <- levels(df$sector) # store original levels
levels(df$sector) <- c("energy", "gen_dis", "cons", "non_ene",
                       "ser_goo", "total") # rename levels
df |> count(sector)
#
# Indicator
df |> 
  count(Indicator) |> 
  arrange(desc(n))
#
# remove Indicators which has less than 810 obs.
df <- df |> 
  group_by(Indicator) |> 
  mutate(n = n()) |> 
  filter(n == 810) |> 
  ungroup()
df |> count(Indicator)
#
# rename Indicator levels
df$Indicator <- as.character(df$Indicator) # un-factor
df$Indicator <- as.factor(df$Indicator) # factor again
levels(df$Indicator) # confirm factor levels
original_indicator <- levels(df$Indicator) # store original factor level name
levels(df$Indicator) <- c("emp_edu", "emp_size", "emp_occ",
                          "hour_edu", "hour_size", "hour_occ",
                          "wage_edu", "wage_size", "wage_occ",
                          "skill_edu", "skill_size", "skill_occ")
df |> count(Indicator)
#
# category
df |> 
  count(category) |> 
  arrange(desc(n))
#
# rename category
df$category <- as.character(df$category) # un factor
df$category <- as.factor(df$category) # factor again
levels(df$category) # confirm levels
original_category <- levels(df$category) # store original category name
levels(df$category) <- c("high", "large", "less", "low", "medium",
                         "middle", "small", "tertiary", "upper")
df |> count(category)
#
# summary of df
summary(df)
#
# remove "n"
df <- df |> 
  select(-n)
df
#


(The top picture is created with Bing Image Creator. Prompt is Photograph of blue sky, a close up of a white apple flower, there is green forest)