我最近刚开始学习R,在尝试使用gather函数时遇到了一个问题。当我将数据帧从宽转换为长时,如何使用gather() fxn来生成包含两列以上的数据帧。
例如下面的代码:
long_iris <- gather(head(iris, 3), "Col_1", "Col_2", "Col_3", "Col_4", "Col_5",1:5)输出此错误消息:
> long_iris <- gather(head(iris, 3), "Col_1", "Col_2", "Col_3", "Col_4", "Col_5",1:5)
Error: Can't subset columns that don't exist.
x Column `Col_3` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.发布于 2021-06-02 09:05:53
使用首选tidyr::pivot_longer
tidyr::pivot_longer(head(iris, 3), -Species)
# # A tibble: 12 x 3
# Species name value
# <fct> <chr> <dbl>
# 1 setosa Sepal.Length 5.1
# 2 setosa Sepal.Width 3.5
# 3 setosa Petal.Length 1.4
# 4 setosa Petal.Width 0.2
# 5 setosa Sepal.Length 4.9
# 6 setosa Sepal.Width 3
# 7 setosa Petal.Length 1.4
# 8 setosa Petal.Width 0.2
# 9 setosa Sepal.Length 4.7
# 10 setosa Sepal.Width 3.2
# 11 setosa Petal.Length 1.3
# 12 setosa Petal.Width 0.2https://stackoverflow.com/questions/67797597
复制相似问题