0% found this document useful (0 votes)
26 views6 pages

First Machine Problem

The document contains code for creating and manipulating different data types in R including matrices, arrays, vectors, factors, and data frames. It also contains code for inputting values from users, calculating factorials, checking if numbers are odd or even, plotting data, importing and exporting CSV files, and plotting hotspot data on a map of Indonesia.

Uploaded by

Nosair ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

First Machine Problem

The document contains code for creating and manipulating different data types in R including matrices, arrays, vectors, factors, and data frames. It also contains code for inputting values from users, calculating factorials, checking if numbers are odd or even, plotting data, importing and exporting CSV files, and plotting hotspot data on a map of Indonesia.

Uploaded by

Nosair ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Submitted by: Abdullah, Hamzah E.

IT 180-XxYy

1. First Machine Problem

# Create a matrix.

M = matrix( c('x','x','u','i','u','x'), num_row=2, num_col=3,byrow = TRUE)

print(M)

# Create an array.

a <- array(c('black','white'), dim=c(3,3,2))

print(a)

# Create a vector.

apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.

factor_apple <- factor(apple_colors)

# Print the factor.

print(factor_apple)

print(nlevels(factor_apple))

# Create the data frame.

BMI <- data.frame(

gender = c("Male", "Male","Female"),

height = c(152, 171.5, 165),

weight = c(81,93, 78),

Age =c(42,38,26)

print(BMI)
# Assignment using equal operator.

var.1 = c(0,1,2,3)

# Assignment using leftward operator.

var.2 <- c("learn","R")

# Assignment using rightward operator.

c(TRUE,1) -> var.3

print(var.1)

cat ("var.1 is ", var.1 ,"\n")

cat ("var.2 is ", var.2 ,"\n")

cat ("var.3 is ", var.3 ,"\n")

var_x <- "Hello"

cat("The class of var_x is ",class(var_x),"\n")

var_x <- 34.5

cat(" Now the class of var_x is ",class(var_x),"\n")

var_x <- 27L

cat(" Next the class of var_x becomes ",class(var_x),"\n")

1. Inputting a number

value = as.integer(readline(prompt="Enter a number: "))

print(paste("The Number You Have Entered Is", value))

2. Factorial

# Take input value from the user.

value = as.integer(readline(prompt="Enter a number: "))

factorial = 1

# check is the number is negative, positive or zero


if(value < 0) {

print("Sorry, factorial does not exist for negative numbers")

} else if(value == 0) {

print("The factorial of 0 is 1")

} else {

for(i in 1:value) {

factorial = factorial * i

print(paste("The factorial of", value ,"is",factorial))

3. Odd or Even program

# Program to check if the input number is odd or even.

# A number is even if division by 2 give a remainder of 0.

# If remainder is 1, it is odd.

value = as.integer(readline(prompt="Enter a number: "))

if((value %% 2) == 0) {

print(paste(value,"is Even"))

} else

print(paste(value,"is Odd"))

4. Plot

# For hop1 project.

var4<- c(1,2,3,4,5,6,7,8,9,10)

var5<- c(1:10)
var6 <- list(1, 2, 3, "MAPFire", "summer", "course")

var7 <- list(var5, var6)

var8 <- c(1, 2, 3, 4, 5)

assign("var9", var8/10)

# or var9 <- var8/10

var10 <- c("Summer", "Course", "MAPFire", "Data" , "Mining")

a <- data.frame(var8, var9, var10)

View(a)

names(a) <- c("Int_type", "Float_Type", "Char_Type")

write.csv(a,"dummyData.csv", row.names = FALSE)

#import csv file

b <- read.csv("dummyData.csv")

print(b)

load("Desktop/(Don't Delete) (Abdullah Hamzah E..)/Hop1/Pollutant_201508.csv")

save(monthyl_data, file="path_directory/data/monthlydata2.Rdata")

library(readr)

data_rd<- read.csv("Desktop/MyProject(Abdullah, Hamzah E.)/Hop1/Pollutant_201508.csv")

print(data_rd)

str(Pollutant_201508.csv)

summary(Pollutant_201508$co)

summary(Pollutant_201508$co2)

# Con’t of Hop1 Project Abdullah, Hamzah E.

library(readr)

data_rd<- read.csv("Desktop/MyProject(Abdullah, Hamzah E.)/Hop1/Pollutant_201508.csv")

print(data_rd)

str(Pollutant_201508)
summary(Pollutant_201508$co)

summary(Pollutant_201508$co2)

###------- Import Hotspot Data -------###

name_file_hotspot<-file.choose()

data_hspot<-as.data.frame((read.csv(file=name_file_hotspot, header=T)))

View(data_hspot)

data_hspot<-data_hspot[,1:3]

data_hspot[,-3]<- round(data_hspot[,-3],3) #the "-3" excludes column 3

names(data_hspot)<-c('lat', 'lon', 'date')

###------Plot Hotspot Data with Indonesian MAP -------###

IDN <- ne_countries(scale = "Medium",

country = 'Indonesia',

returnclass = "sf")

ggplot(data=IDN) + geom_sf()+

geom_point(data_hspot, mapping = aes(lon, lat), colour='red')

###------- Import Hotspot Data -------###

name_file_hotspot<-file.choose()

data_hspot<-as.data.frame((read.csv(file=name_file_hotspot, header=T)))

data_hspot<-data_hspot[,1:3]

data_hspot[,-3]<- round(data_hspot[,-3],3) #the "-3" excludes column 3

names(data_hspot)<-c('lat', 'lon', 'date')

name_file_raster<-file.choose()

data_raster<-as.data.frame((read.csv(file=name_file_raster, header=T)))
###------Plot Hotspot Data with Indonesian MAP -------###

IDN <- ne_countries(scale = "Medium",

country = 'Indonesia', returnclass = "sf")

temp_colours <- c("white","blue","green","yellow","red")

prec_colours <- c("red","yellow","green","blue","white")

ggplot(data=IDN) +

geom_sf()+

geom_tile(data_raster, mapping=aes(lon, lat, fill=prec))+

scale_fill_gradientn(colours= prec_colours)+

geom_point(data_hspot, mapping = aes(lon, lat), colour='red')

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy