r - 如何根据字符串绑定列
问题描述
我正在尝试根据列的字符串将我的数据框从宽格式转换为长格式。在下面的示例中,我想要在单个列中,O2_222.coefficients.x1
并且在单个列中并且作为它自己的。尝试在包中使用,但似乎无法正确使用语法。O2_217.coefficients.x1
O2_222.R-squared
O2_217.R-squared
RunTime
melt
reshape2
structure(list(O2_222.coefficients.x1 = c(0.494206524044508, 0.351865091962266,
0.348933739038027, 0.412232161883577, 0.702783684327072), `O2_222.R-squared` =
c(0.922054236839182, 0.915753625911676, 0.91109476704698, 0.917998834313392,
0.967759465780247), O2_217.coefficients.x1 = c(0.390012278483346, 0.0694948285748309,
0.0323121611694059, 0.0372526286990146, 0.194291648564898), `O2_217.R-squared` =
c(0.921256057864199, 0.537913087580067, 0.271398305115866, 0.274339042666519,
0.908338928665188), RunTime = c(9.03, 14.08, 19.08, 24.08, 29.08)), row.names = c(116L,
216L, 316L, 416L, 516L), class = "data.frame")
解决方案
我们可以使用pivot_longer
fromtidyr
并指定names_sep
as.
library(tidyr)
library(dplyr)
df1 %>%
pivot_longer(cols = -RunTime, names_to = c('group', '.value'), names_sep="\\.")
# A tibble: 10 x 4
# RunTime group coefficients `R-squared`
# <dbl> <chr> <dbl> <dbl>
# 1 9.03 O2_222 0.494 0.922
# 2 9.03 O2_217 0.390 0.921
# 3 14.1 O2_222 0.352 0.916
# 4 14.1 O2_217 0.0695 0.538
# 5 19.1 O2_222 0.349 0.911
# 6 19.1 O2_217 0.0323 0.271
# 7 24.1 O2_222 0.412 0.918
# 8 24.1 O2_217 0.0373 0.274
# 9 29.1 O2_222 0.703 0.968
#10 29.1 O2_217 0.194 0.908
推荐阅读
- c# - 如何隐藏/更改起始页中的 URL?
- android - Android 自动填充服务在 Chrome 中不起作用
- charts - 如何在pinescript中最低点之后的下一个最高点
- python - 如何制作简单的地图编辑器或关卡编辑器?
- amazon-web-services - 使用 AWS cli 将自定义域迁移到不同的 API 终端节点时出现问题
- javascript - Woocommerce 订单动态输出数据在 WordPress Thickbox 中不起作用
- heidisql - 如何在 Heidi sql 中从多个具有条件的表中选择 *
- r - 绘制> 1000点时如何提高ggplot条形图的速度?
- php - 根据 WooCommerce 订单商品产品标签更改 PayPal 电子邮件
- html - css动画的问题