R语言,data

R语言基本指令,本文主要focus在数据结构上

接上篇
本文主要是不同的数据类型以及基本的结构,主要参考的是datacamp练习

##vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
***表示vector*** c(里面跟vector)
***name a vector***:some_vector <- c("John Doe", "poker player")
names(some_vector) <- c("Name", "Profession")
#we will get
Name Profession
"John Doe" "poker player"
***比较大小***
To check if 6 is larger than 5, you type 6 > 5. This returns a logical value (TRUE or FALSE).
***选择数据***
select the first element of the vector, you type poker_vector[1].select the second element of the vector, you type poker_vector[2]
(注意顺序和python中不一样,第一个数字就type1)
***选择roulette_vector中大于0的时候***
selectionvector <- roulette_vector>0
***选择roulette_vector中大于0的时候并显示数额***
roulette_winning_days <- roulette_vector[selection_vector]

##matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
***form a matrix***
matrix(1:9, byrow = TRUE, nrow = 3)
box_office <- c(new_hope, empire_strikes, return_jedi) , where new_hope, empire_strikes, return_jedi are vectors
***add a name for matrix***
rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector
*** calculates the totals for each row of a matrix***
rowSums(my_matrix) #if sum a column, colSums()
*** add column to a matrix***
big_matrix <- cbind(matrix1, matrix2, vector1 ...)(注意顺序以及matrix的demension) #if adding a row, just use rbind
***选择数据***
my_matrix[1,2] selects the element at the first row and second column.
my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.
my_matrix[,1] selects all elements of the first column.
my_matrix[1,] selects all elements of the first row.
***matrix运算***
2 * my_matrix multiplied every element of my_matrix by two
my_matrix1 * my_matrix2 creates a matrix where each element is the product of the corresponding elements in my_matrix1 and my_matrix2

factor

1
2
3
4
5
6
7
***change the names of specific factor levels***
levels(factor_vector) <- c("name1", "name2",...)
***create an ordered factor***
you have to add two additional arguments: ordered and levels.
factor(some_vector,
ordered = TRUE,
levels = c("lev1", "lev2" ...))

data frames

1
2
3
4
5
6
7
8
9
10
11
12
***insight of the data structure***
head() show the first observations of a data frame the function
tail() prints out the last observations in your data set.
***construct a dataframe***
use data.frame()
planets_df <-data.frame(name,type,diameter,rotation,rings)
where name, type...are vectors
***select elements***
like matrix
planets_df[1:3,"type"] #look up for the first elements of column type
planets_df$diameter #select the whole diameter column
subset(planets_df, subset= diameter <1) #选择diameter<1的星球

lists

1
2
3
4
5
6
7
8
9
10
11
12
13
14
***construct a list***
my_list <- list(comp1, comp2 ...) #comp1 comp2 are vectors
***give names to your list***
my_list <- list(name1 = your_comp1,
name2 = your_comp2)
or:
my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")
***select***
shining_list[[1]] #"grab" the first component of shining_list
shining_list[["reviews"]]
shining_list$reviews
#The three instructions do the same
shining_list$actors[2] #choose the second element of actors