-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rmarkdown.Rmd
102 lines (76 loc) · 3.02 KB
/
Rmarkdown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
title: "Quantitative assessments"
author: "Olufemi Olamijulo"
date: "2024-01-09"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r, echo=FALSE}
library(readxl)
library(dplyr)
library(tidyr)
```
#read in Dataset
```{r}
Df <- read_excel("GlobalLandCove_ComputeChange_ExportTable_TableToExcel.xlsx")
```
```{r}
# Make sure that df is indeed a dataframe or tibble
Df <- as.data.frame(Df)
# Use dplyr's mutate to categorize 'Class_From' and remove rows with "Same"
Cleaned_Data <- Df %>%
dplyr::mutate(Class = dplyr::case_when(
Class_From == "Same" ~ NA_character_,
Class_From %in% c("Closed Canopy Broadleaved Deciduous Tree Cover",
"Closed to Open Canopy Broadleaved Deciduous Tree Cover",
"Closed to Open Canopy Broadleaved Evergreen Tree Cover",
"Open Canopy Broadleaved Deciduous Tree Cover",
"Mostly Trees and Shrubs in a Mosaic with Herbaceous Cover") ~ "forest",
Class_From %in% c("Bodies of water",
"Flooded Shrub or Herbaceous Cover",
"Fresh or Brackish Water Flooded Tree Cover",
"Saline Water Flooded Tree Cover") ~ "water body",
Class_From %in% c("Herbaceous Cropland",
"Irrigated or Post-Flooding Cropland",
"Mostly Cropland in a Mosaic with Natural Vegetation",
"Mostly Natural Vegetation in a Mosaic with Cropland",
"Rainfed Cropland") ~ "farmland",
Class_From %in% c("Mostly Herbaceous Cover in a Mosaic with Trees and Shrubs",
"Shrubland",
"Sparse Vegetation",
"Grassland") ~ "grassland",
TRUE ~ as.character(Class_From)
)) %>%
filter(!is.na(Class)) %>%
dplyr::select(-OBJECTID, -Value, -Classvalue, -Class_name, -Class_From, -Class_To, -Red, -Green, -Blue, -Alpha, -Count)
# Check the first few rows of Cleaned_Data
head(Cleaned_Data)
```
```{r}
# Move the Class column to the first position
Cleaned_Data <- Cleaned_Data %>%
select(Class, everything())%>%
group_by(Class) %>%
summarise(across(where(is.numeric), sum, na.rm = TRUE))
```
```{r}
file_path <- "cleaned.csv"
# Export the data frame to a CSV file
write.csv(Cleaned_Data, file = file_path, row.names = FALSE)
```
```{r}
```