通过网络查询连接函数使用方法,当关键字列名不一样时,如何代码实现? 例如: credit_t <- tibble(课程名 = colnames(score_t)[-1]) |> left_join(credit, by = _________) credit_t
时间: 2023-05-17 10:07:05 浏览: 123
可以使用参数 `by.x` 和 `by.y` 来指定连接的列名不一样时的对应关系,具体代码实现如下:
credit_t <- tibble(课程名 = colnames(score_t)[-1]) |>
left_join(credit, by = c("课程名" = "course_name"))
相关问题
运行下列代码,将基于 stringr:: words 的数据生成一个tibble,命名为tb_wordstb_words <- tibble( index = seq_along(stringr::words), words = stringr::words)
好的,可以运行以下代码将 `stringr::words` 生成的数据转化为 `tibble` 格式,并命名为 `tb_words`:
```R
library(tibble)
library(stringr)
tb_words <- tibble(
index = seq_along(stringr::words),
words = stringr::words
)
```
这里使用了 `seq_along()` 函数来生成一个整数序列,作为表格的第一列 `index`。同时,使用 `stringr::words` 函数生成单词向量,并将其作为表格的第二列 `words`。最后将这两列合并成一个 `tibble`,命名为 `tb_words`。
student_scores <- tibble::tribble( ~student_id, ~math_score, ~english_score, ~science_score, ~sports_score, 1, 75, 80, 85, 90, 2, 90, 85, 70, 75, 3, 80, 70, 75, 85, 4, 85, 90, 80, 70, 5, 70, 75, 90, 80 ) 提取上述数据的主成分,用recipes包实现。
好的,以下是使用`recipes`包提取主成分的代码:
```R
library(recipes)
# 创建数据框
student_scores <- tibble::tribble(
~student_id, ~math_score, ~english_score, ~science_score, ~sports_score,
1, 75, 80, 85, 90,
2, 90, 85, 70, 75,
3, 80, 70, 75, 85,
4, 85, 90, 80, 70,
5, 70, 75, 90, 80
)
# 创建recipe对象
student_recipe <- recipe(~., data = student_scores) %>%
step_center(all_predictors()) %>%
step_scale(all_predictors()) %>%
step_pca(all_predictors(), num_comp = 2)
# 应用recipe对象
student_pca <- student_recipe %>% prep() %>% bake(new_data = NULL)
# 打印结果
student_pca
```
其中,`step_center`和`step_scale`用于对所有预测变量进行中心化和标准化,`step_pca`用于提取两个主成分。最后,`prep()`和`bake()`函数用于应用和执行recipe对象。