1students3 %>%2gather(class, grade, class1:class5, na.rm = TRUE) %>%3#name未参与gather,因此照原格式输出4spread(test, grade) %>%5#spread:Spread a key-value pair across multiple columns.6mutate(class=parse_number(class)) %>%7#parse_numeric Extract numeric component of variable.8print...
这让 dplyr 代码有两个主要优点:Note that the echo = FALSE parameter was added to the code chun...
# Replace NA in column StudyHours with the mean study hours # Drop NAs from our tibble anyNA(students) # Get the mean study hours using the accessor `$` # Get students who studied for more than the average number of hours # Mean grade of students who studied more than averag...
column_to_rownames():列转成行名 2. 两个表操作 两表及多表操作是数据处理中比较常用的操作,跟SQL关联类似。 bind_cols():合并列 left_join():左关联,同SQL的left outer join 左表匹配右表,返回左表。 data1 %>% left_join(data2,by=c("id"="id")) inner_join():内关联,同SQL的inner join ...
在dplyr0.7.0+中正确使用dplyr::select,使用字符向量选择列 、、、 3, e=1:3)假设我们还想使用dplyr::select,因为它是使用%>%的操作的一部分,所以使用select使代码易于阅读。dplyr::select(df,cols_to_select) #Fails if 'cols_to_select' happens to be the name of a column 浏览...
# Replace NA in column StudyHours with the mean study hours # Drop NAs from our tibble anyNA(students) # Get the mean study hours using the accessor `$` # Get students who studied for more than the average number of hours # Mean grade of students who studied more than av...
我们可以使用 add_row、add_column 函数,添加观测值、添加 变量。类似于 rbind、cbind 函数,但这两个函数还可以在原数据框的 任意位置插入行列。注意这两个函数是根据 tibble 类型的数据框实现 的,不支持使用行名;此外 add_row 会对列名进行更加严格的检查, 所以需要我们按照列名添加观测值。
select(-species) %>% mutate(order = row_number(mass), ntile = ntile(height,n = 2), diff_heigt = c(NA,diff(height))) ## # A tibble: 10 x 6 ## name height mass order ntile diff_heigt ## <chr> <int> <dbl> <int> <int> <int> ...
# Keep all columns except the StudyHours column # Select numeric columns # Create a data frame with the data sorted by Grade (descending) 无计算 计算 未连接 查看 内核未连接 下一單元: 將資料視覺化 上一個 下一步 是否需要協助? 請參閱我們的疑難排解指南,或透過回報問題提供具體...
# Replace NA in column StudyHours with the mean study hours # Drop NAs from our tibble anyNA(students) # Get the mean study hours using the accessor `$` # Get students who studied for more than the average number of hours # Mean grade of students who studied more than average ...