-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_Tutorial_Basic.R
More file actions
84 lines (63 loc) · 2.33 KB
/
R_Tutorial_Basic.R
File metadata and controls
84 lines (63 loc) · 2.33 KB
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
closeAllConnections()
rm(list=ls())
Died.At <- c(22,40,72,41)
Writer.At <- c(16, 18, 36, 36)
First.Name <- c("John", "Edgar", "Walt", "Jane")
Second.Name <- c("Doe", "Poe", "Whitman", "Austen")
Sex <- c("MALE", "MALE", "MALE", "FEMALE")
Date.Of.Death <- as.Date(c("2015-05-10", "1849-10-07", "1892-03-26","1817-07-18"))
writers_df <- data.frame(Died.At, Writer.At, First.Name, Second.Name, Sex, Date.Of.Death)
str(writers_df)
head(writers_df)
summary(writers_df)
names(writers_df)
names(writers_df) <- c("Age.At.Death", "Age.As.Writer", "Name", "Surname", "Gender", "Death")
names(writers_df)
dim(writers_df)
rows <- dim(writers_df)[1]
columns <- dim(writers_df)[1]
columns <- length(writers_df)
third.column <- writers_df[1:2,3]
writers.name <- cbind(First.Name, Second.Name)
writers.two <- rbind(writers_df, writers_df)
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
apply(x, 2, mean, trim = .2)
col.sums <- apply(x, 2, sum)
row.sums <- apply(x, 1, sum)
Age <- numeric()
Name <- character()
ID <- integer()
Gender <- factor()
Date <- as.Date(character())
ab <- data.frame(Age, Name, ID, Gender, Date)
str(ab)
writer_names_df <- writers_df[1:4, "Name", drop=FALSE]
young_writers_df <- subset(writers_df, Age.At.Death <= 40 & Age.As.Writer >= 18)
forty_sth_writers <- writers_df[writers_df$Age.At.Death > 40,]
writers_df$Age.As.Writer = ifelse(writers_df$Age.As.Writer>30,40,20)
writers_df.mean <- aggregate(Age.At.Death~Gender, writers_df, mean, na.rm = TRUE)
writer_names_df2 <- data.frame(a = seq(1,16,by=2), b = LETTERS[1:8], x= month.abb[1:8], y = sample(10:20,8, replace = TRUE), z=letters[1:8])
Age.At.Death <- NULL
writers_df$Location <- c("Belgium", "United Kingdom", "United States", "United Kingdom")
library(taRifx)
sorted_data <- sort(writers_df, decreasing=TRUE, ~Age.At.Death)
library(dplyr)
food <- factor(c("low", "high", "medium", "high", "low", "medium", "high"))
levels(food)
print(is.factor(food))
nlevels(food)
print("Unordered Factors:")
food <- factor(food, levels = c("low", "medium", "high"))
levels(food)
print("Ordered factors")
food <- factor(food, levels = c("low", "medium", "high"), ordered = TRUE, exclude = NULL)
levels(food)
print(is.ordered(food))
min(food)
max(food)
print("Converting factors")
num<-as.numeric(food)
print(food)
print(num)
print("Using factors")
dat <- read.csv(file = 'data/sample.csv', stringsAsFactors = TRUE)