Rewriting code using dplyr
In the previous chapter, R was used to find the estimate of the total road length in 2011
. Here are the steps that were completed in the previous chapter, written using dplyr
verbs:
Filter
out the rows with a mean greater than2000
Filter
out the rows in which all values are NAMutate
the2011
column to create a copy in which the NA values are replaced with the row meanSelect
the new2011
column and find the sum of its values
At the beginning of dplyr_intro.R
, the first step should be to read artificial_roads_by_region.csv
to an R dataframe as follows:
roads.lengths <- read.csv("data/artificial_roads_by_region.csv")
Next, In the following continuation of dplyr_intro.R
, a copy of the original roads length data called roads.length2
is created. The row averages and the row sums of the roads.length2
dataframe are calculated and added as columns to the dataframe. These columns will help with the filtering steps.
roads.lengths2<-roads.lengths roads.lengths2$mean_val ...