I have this data frame and I'm trying to create a new data frame from the first column.
> 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")
I used do.call and rbind to convert to a dataframe
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")
I apologize for not removing the column R did not like it when i removed the others.
I'm trying to use grep to break the 1st 2 vectors (row names and column 1) into my desired columns based on the first character of the row name.
test <- cut(test[,0],
breaks = c(grep("r"), grep("n"), grep("P")),
labels = c("r", "n", "P"),
right = FALSE)
So basically my df would look like this with rounded values (another thing I clearly didn't know how to do with dput). this would use the first letter of the row name for the new column names.
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
I could not find a single post about cutting or breaking by row names in r.
I'm sure some smart soul on here can accomplish this in a much cleaner way straight from the initial list, but that person is clearly not me.
Again I apologize for the long data and number of digits. Thanks.
Without converting the test
data to a single data.frame
with do.call
, the desired output can be obtained with:
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