2_vector 20mins
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
**names**(poker_vector) <- days_vector
names(roulette_vector) <- days_vector
# Which days did you make money on roulette?
**selection_vector <- roulette_vector > 0**
# Select from roulette_vector these days
roulette_winning_days <- roulette_vector[selection_vector]
roulette_winning_days
**sum**(roulette_winning_days)
3_Matrics 30mins
# all_wars_matrix and ticket_prices_matrix are available in your workspace
all_wars_matrix
ticket_prices_matrix
# Estimated number of visitors
visitors <- all_wars_matrix / ticket_prices_matrix
# US visitors
us_visitors <- visitors[ ,1]
# Average number of US visitors
mean(us_visitors)
colSums rowSums 注意!
matrix依然是同一类型
4_Factors 20mins
# Create factor_speed_vector
speed_vector <- c("medium", "slow", "slow", "medium", "fast")
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))
# Factor value for second data analyst
da2 <- factor_speed_vector[2]
# Factor value for fifth data analyst
da5 <- factor_speed_vector[5]
# Is data analyst 2 faster than data analyst 5?
da2 > da5
summary(factor)
factor(vector,ordered=,levels=c())
factor[1]
factor is for categorical variables
5_Dataframe
A data frame has the variables of a data set as columns and the observations as rows. This will be a familiar concept for those coming from different statistical software packages such as SAS or SPSS.
Applying the str() function will often be the first thing that you do when receiving a new data set or data frame. It is a great way to get more insight in your data set before diving into the real analysis.
planets_df[rings_vector, ]
subset(planets_df,diameter<1)
6_lists
Vectors (one dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.
Matrices (two dimensional array): can hold numeric, character or logical values. The elements in a matrix all have the same data type.
Data frames (two-dimensional objects): can hold numeric, character or logical values. Within a column all elements have the same data type, but different columns can be of different data type.
You could say that a list is some kind super data type: you can store practically any piece of information in it!
select: list[[ ]] such as : list[[2]][1]
Conclusion
- 数据类型:vector matrix dataframe list
- select:list有区别,是用[[]]
- 其他:order summary str names