-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession2_dataFrames.R
executable file
·33 lines (23 loc) · 1.04 KB
/
session2_dataFrames.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
# Manipulating data frames
# Read in a data frame from file.
# Write a comma-deliminated file by "File -> New File -> Text File".
dat <- read.table(file = "example1.csv", header = TRUE, sep = ",")
# Subsetting a data frame is very similar with subsetting a matrix.
typeof(dat[2,])
class(dat[,"height"])
# You can use the $ to subset by names
dat$color # characters are by default stored as factors.
dat <- read.table(file = "example1.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)
dat$color <- as.character(dat$color)
class(t(dat))
# Subsetting with logical operations
dat[dat$color == "red", ]
# Subset using the function subset
subset(dat, color == "red") # columns can be referred to (by name) as variables in the function call
# Adding columns/rows to data frames.
name <- c("a", "b", "c", "d")
dat <- cbind(dat, name)
dat$name2 <- c("e", "f", "g", "h")
dat[, c("height", "weight", "name", "color")]
### Write data frame to a file.
write.table(dat, file = "example_modified.txt", col.names = TRUE, row.names = FALSE, quote = FALSE, sep = "\t")