-
Notifications
You must be signed in to change notification settings - Fork 0
/
R code 2.R
130 lines (82 loc) · 2.51 KB
/
R code 2.R
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
getwd()
setwd("C:\\Users\\welcome\\Documents\\ExcelR\\Day 05 Basic Stat _ R\\Data Sets\\") # set a working directory of your choice
victims <- readLines("C:\\Users\\welcome\\Documents\\ExcelR\\Day 05 Basic Stat _ R\\Data Sets\\victims.txt")
victims
?as
df <- as.data.frame (victims)
df
class(df)
?length
length(df$victims)
nrow(df)
ncol(df)
dim(df)
str(df)
#https://rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf
#https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html
?grepl
comments <- grepl("^%", victims)
comments
comments <- grepl("\\d+", victims)
comments
text <- victims[!comments]
text
victims
comments_grepl <- grepl("^%", victims)
comments_grepl
comments_grep <- grep ("^%", victims)
comments_grep
text_grep <- victims[!comments_grep]
text_grep
text[1]
x <- text[1]
x
y <- sub ("[[:digit:]]", "", x) # (pattern, replacement, x)
y
text[1]
x <- text[1]
y <- gsub ("\\d+", "", x)
y
text[9]
r <- regexpr("9", text[9])
r
r <- gregexpr("9", text[9])
r
text
splitlines <- strsplit(text, split = ",")
splitlines
Lines <- matrix (unlist(splitlines), nrow=length(splitlines), byrow = TRUE)
Lines
colnames(Lines) <- c("Name", "BirthYear", "DeathYear")
#in a data frame should be treated as factor variables or as just plain strings
titanic_victims <- as.data.frame(Lines, stringsAsFactors = FALSE)
titanic_victims
class(titanic_victims$BirthYear)
titanic_victims <- as.data.frame(Lines, stringsAsFactors = TRUE)
titanic_victims$DeathYear
class(titanic_victims$BirthYear)
titanic_victims$BirthYear <- as.numeric(titanic_victims$BirthYear)
titanic_victims$BirthYear
titanic_victims <- transform (titanic_victims, BirthYear = as.numeric(BirthYear),
DeathYear = as.numeric(DeathYear))
class(titanic_victims$BirthYear)
class(titanic_victims$DeathYear)
titanic_victims
str(titanic_victims)
mean(titanic_victims$BirthYear)
round(mean(titanic_victims$BirthYear))
#=============================================================================
install.packages("lattice")
library(lattice)
data(barley)
View(barley)
dim(barley)
str(barley)
?lapply
lapply(barley, function(x) length(unique(x)))
sapply(barley, function(x) length(unique(x)))
apply(barley, 2,function(x) length(unique(x)))
?apply
levels(as.factor(barley$site))
tapply (barley$yield, barley$site, sum)
tapply (barley$yield, barley$site, mean)