library(tidyverse) #调用tidyverse库,需要提前安装该哭 install.packages("tidyverse")
leadership <- data.frame(
manager = c(1, 2, 3, 4, 5),
date = c("10/24/08", "10/28/08", "10/1/08", "10/12/08", "5/1/09"),
country = c("US", "US", "UK", "UK", "UK"),
gender = c("M", "F", "F", "M", "F"),
age = c(32, 45, 25, 39, 99),
q1 = c(5, 3, 3, 3, 2),
q2 = c(4, 5, 5, 3, 2),
q3 = c(5, 2, 5, 4, 1),
q4 = c(5, 5, 5, NA, 2),
q5 = c(5, 5, 2, NA, 1),
datas = c(Sys.Date()+1,Sys.Date()+2,Sys.Date()+3,Sys.Date()+4,Sys.Date()+5)
)
#数据求和 to total each q1~5
total_score = leadership$q1+leadership$q2+leadership$q3+leadership$q4+leadership$q5
total_score
#显示数据框数据 show the dataframe of leadership
leadership
#去除数据框的某列数据remove age coloumn
newdata <-leadership[order(leadership$gender, -leadership$age),]
newdata
#列出特定列的数据如:list data q1~5
myvars <- paste("q", 1:5, sep="")
newdata <- leadership[myvars]
newdata
#随机抽取指定数量的行 random select rows
mysample <- leadership[sample(1:nrow(leadership), 3, replace=FALSE),]
mysample
#选取数据框的部分列(-表示去掉某列)
leadership_subset <- select(leadership, country:-age, starts_with("q"))
leadership_subset
#条件过滤 dataframe <- filter(dataframe, expression)
extreme_men <- filter(leadership, gender == "M" & (q1 < 2 | q3> 4))
extreme_men
#arrang() 排序数据
leadership <- arrange(leadership,gender, desc(age))
leadership