[go: up one dir, main page]

0% found this document useful (0 votes)
189 views2 pages

R Basics

The document contains 10 coding exercises in R for working with vectors, matrices, dates, random numbers, NA values, conditional operations, and for loops. It demonstrates how to create and manipulate vectors and matrices, convert between categorial and numeric variables, perform vector operations and random number generation, handle missing values, use which() for conditional operations, and iterate through a vector with a for loop to calculate a sum. The exercises cover basic tasks in R like data wrangling, data types, and control flow.

Uploaded by

Jigar Desai
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)
189 views2 pages

R Basics

The document contains 10 coding exercises in R for working with vectors, matrices, dates, random numbers, NA values, conditional operations, and for loops. It demonstrates how to create and manipulate vectors and matrices, convert between categorial and numeric variables, perform vector operations and random number generation, handle missing values, use which() for conditional operations, and iterate through a vector with a for loop to calculate a sum. The exercises cover basic tasks in R like data wrangling, data types, and control flow.

Uploaded by

Jigar Desai
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/ 2

1.

Create a vector variable with the values  "yes", "no", "yes", "maybe

# Enter your code here. Read input from STDIN. Print output to STDO
UT
V <-factor(c("yes","no","yes","maybe"))
levels(V)

R Basics-2 - Coding- Date and Time in R


# Enter your code here. Read input from STDIN. Print output to STDO
UT
s= "25-12-2016"
as.Date(s,format("%d-%m-%Y"))

3. Coding-Vector Operations
V1 <- c(1:5)
V2 <- c(10,20,30,40,50)
print(V1 + V2)

4. R Basics-4 - Coding-Vectors and Matrices


V <- c(1:9)
M <- matrix(V, byrow =TRUE, nrow = 3)
print (M * 2)

5. R Basics-5 - Coding-Categorial to Numeric Variable


v1 <- factor (round(runif(10,10,70)))
v2 <- as.numeric(as.character(v1))
v1 == v2

6. R Basics-6 -Coding- Random Numbers


# Enter your code here. 
V <- runif(10,0,1)
V <- round(V, digits=2)
V < 0.50
7. R Basics-7 - Coding-Handling 'NA' 
x <- c(1, 4, NA, 7, 9, NA, 2)

x[!(is.na(x) | x %% 2 == 1)]

8. R Basics-11 - Coding-Conditional Operations- which() 

9. R Basics-12 - Coding- for() Loops in R

V <- 80:100
sum <- 0
y = V[which(V %% 2 == 0)]
for(i in y){sum=sum + i}
print(sum)

10.

You might also like