在行名称中使用 grep 剪切数据框

cut data frame using grep in row names

我有这个数据框,我正在尝试从第一列创建一个新的数据框。

> dput(test)
structure(list(r = structure(c(1, -0.242175528061635, -0.0666583192103071, 
0.747898462006041, -0.273127322801604, -0.854174676122907, -0.326472398494847, 
-0.242175528061635, 1, 0.610093319116511, -0.716174286974882, 
0.369971592520776, 0.508225738755394, -0.696867613186299, -0.0666583192103071, 
0.610093319116511, 1, -0.701926352509189, -0.38572193914295, 
-0.559677556010383, -0.125875964382533, 0.747898462006041, -0.716174286974882, 
-0.701926352509189, 1, 0.269630040258706, -0.614852282668006, 
0.230479322245116, -0.273127322801604, 0.369971592520776, -0.38572193914295, 
0.269630040258706, 1, 0.0600998872561921, 0.116913308995612, 
-0.854174676122907, 0.508225738755394, -0.559677556010383, -0.614852282668006, 
0.0600998872561921, 1, 0.0794305579734066, -0.326472398494847, 
-0.696867613186299, -0.125875964382533, 0.230479322245116, 0.116913308995612, 
0.0794305579734066, 1), .Dim = c(7L, 7L), .Dimnames = list(c("Age0", 
"Age1", "Age2", "Age3", "Age4", "Age5", "Age6"), c("Age0", "Age1", 
"Age2", "Age3", "Age4", "Age5", "Age6"))), n = structure(c(11L, 
10L, 9L, 7L, 7L, 6L, 5L, 10L, 10L, 9L, 7L, 7L, 6L, 5L, 9L, 9L, 
9L, 7L, 7L, 6L, 5L, 7L, 7L, 7L, 7L, 6L, 6L, 5L, 7L, 7L, 7L, 6L, 
7L, 6L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L), .Dim = c(7L, 7L), .Dimnames = list(c("Age0", "Age1", "Age2", 
"Age3", "Age4", "Age5", "Age6"), c("Age0", "Age1", "Age2", "Age3", 
"Age4", "Age5", "Age6"))), P = structure(c(NA, 0.500232797835481, 
0.864706601629758, 0.0532193933767151, 0.553424124702133, 0.0303470480407344, 
0.591829340417803, 0.500232797835481, NA, 0.0810438517167933, 
0.0702490823574873, 0.414004850479571, 0.303297069271903, 0.19097470996879, 
0.864706601629758, 0.0810438517167933, NA, 0.078730337182463, 
0.392783486027188, 0.248140075649853, 0.840153996654778, 0.0532193933767151, 
0.0702490823574873, 0.078730337182463, NA, 0.605356039921348, 
0.193941978043868, 0.709163811251702, 0.553424124702133, 0.414004850479571, 
0.392783486027188, 0.605356039921348, NA, 0.909958709405365, 
0.851481168303219, 0.0303470480407344, 0.303297069271903, 0.248140075649853, 
0.193941978043868, 0.909958709405365, NA, 0.898972319533829, 
0.591829340417803, 0.19097470996879, 0.840153996654778, 0.709163811251702, 
0.851481168303219, 0.898972319533829, NA), .Dim = c(7L, 7L), .Dimnames = list(
    c("Age0", "Age1", "Age2", "Age3", "Age4", "Age5", "Age6"), 
    c("Age0", "Age1", "Age2", "Age3", "Age4", "Age5", "Age6")))), class = "rcorr")

我使用 do.call 和 rbind 转换为数据帧

test <- do.call(rbind.data.frame, test)
> dput(test)
structure(list(Age0 = c(1, -0.242175528061635, -0.0666583192103071, 
0.747898462006041, -0.273127322801604, -0.854174676122907, -0.326472398494847, 
11, 10, 9, 7, 7, 6, 5, NA, 0.500232797835481, 0.864706601629758, 
0.0532193933767151, 0.553424124702133, 0.0303470480407344, 0.591829340417803
), Age1 = c(-0.242175528061635, 1, 0.610093319116511, -0.716174286974882, 
0.369971592520776, 0.508225738755394, -0.696867613186299, 10, 
10, 9, 7, 7, 6, 5, 0.500232797835481, NA, 0.0810438517167933, 
0.0702490823574873, 0.414004850479571, 0.303297069271903, 0.19097470996879
), Age2 = c(-0.0666583192103071, 0.610093319116511, 1, -0.701926352509189, 
-0.38572193914295, -0.559677556010383, -0.125875964382533, 9, 
9, 9, 7, 7, 6, 5, 0.864706601629758, 0.0810438517167933, NA, 
0.078730337182463, 0.392783486027188, 0.248140075649853, 0.840153996654778
), Age3 = c(0.747898462006041, -0.716174286974882, -0.701926352509189, 
1, 0.269630040258706, -0.614852282668006, 0.230479322245116, 
7, 7, 7, 7, 6, 6, 5, 0.0532193933767151, 0.0702490823574873, 
0.078730337182463, NA, 0.605356039921348, 0.193941978043868, 
0.709163811251702), Age4 = c(-0.273127322801604, 0.369971592520776, 
-0.38572193914295, 0.269630040258706, 1, 0.0600998872561921, 
0.116913308995612, 7, 7, 7, 6, 7, 6, 5, 0.553424124702133, 0.414004850479571, 
0.392783486027188, 0.605356039921348, NA, 0.909958709405365, 
0.851481168303219), Age5 = c(-0.854174676122907, 0.508225738755394, 
-0.559677556010383, -0.614852282668006, 0.0600998872561921, 1, 
0.0794305579734066, 6, 6, 6, 6, 6, 6, 5, 0.0303470480407344, 
0.303297069271903, 0.248140075649853, 0.193941978043868, 0.909958709405365, 
NA, 0.898972319533829), Age6 = c(-0.326472398494847, -0.696867613186299, 
-0.125875964382533, 0.230479322245116, 0.116913308995612, 0.0794305579734066, 
1, 5, 5, 5, 5, 5, 5, 5, 0.591829340417803, 0.19097470996879, 
0.840153996654778, 0.709163811251702, 0.851481168303219, 0.898972319533829, 
NA)), row.names = c("r.Age0", "r.Age1", "r.Age2", "r.Age3", "r.Age4", 
"r.Age5", "r.Age6", "n.Age0", "n.Age1", "n.Age2", "n.Age3", "n.Age4", 
"n.Age5", "n.Age6", "P.Age0", "P.Age1", "P.Age2", "P.Age3", "P.Age4", 
"P.Age5", "P.Age6"), class = "data.frame")

我很抱歉没有删除我删除其他列时 R 不喜欢的列。

我正在尝试使用 grep 根据行名的第一个字符将前 2 个向量(行名和第 1 列)分解为我想要的列。

test  <- cut(test[,0], 
                            breaks = c(grep("r"), grep("n"),  grep("P")), 
                            labels = c("r", "n", "P"), 
                            right = FALSE)

所以基本上我的 df 看起来像这样四舍五入的值(另一件事我显然不知道如何处理 dput)。这将使用行名的第一个字母作为新列名。

         r   n     P
Age0  1.00  11    NA
Age1 -2.42  10  0.50
Age2 -0.07   9  0.86
Age3  0.75   8  0.05
Age4 -0.27   7  0.55
Age5 -0.85   6  0.03
Age6 -0.33   5  0.59

我在 r.

中找不到关于按行名称切割或打断的单个 post

我敢肯定这里有些聪明人可以直接从初始列表中以更清晰的方式完成此操作,但那个人显然不是我。

对于长数据和位数,我再次表示歉意。谢谢。

无需使用 do.calltest 数据转换为单个 data.frame,即可通过以下方式获得所需的输出:

as.data.frame(lapply(test, function(x) x[1, ]))
#                r  n          P
# Age0  1.00000000 11         NA
# Age1 -0.24217553 10 0.50023280
# Age2 -0.06665832  9 0.86470660
# Age3  0.74789846  7 0.05321939
# Age4 -0.27312732  7 0.55342412
# Age5 -0.85417468  6 0.03034705
# Age6 -0.32647240  5 0.59182934

在 data.frame 上使用 tidyverse:

library(dplyr)
library(tidyr)
test %>%
  add_rownames(var = "rowname") %>%
  separate(rowname, c("Var", "Age")) %>%
  select(Var, Age, Age0) %>%
  pivot_wider(names_from = Var, values_from = Age0)