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

xj3cbfub  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(131)

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

> 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将其转换为 Dataframe
x一个一个一个一个x一个一个二个x
我很抱歉没有删除列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中按行名称进行剪切或中断的文章。
我相信这里的一些聪明人可以直接从最初的列表中以更干净的方式完成这一点,但那个人显然不是我。
再次为冗长的数据和数字表示歉意。谢谢。

kx5bkwkv

kx5bkwkv1#

无需将test数据转换为单个data.framedo.call,即可获得所需的输出:

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
but5z9lq

but5z9lq2#

在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)

相关问题