Reputation: 7517
I'm trying to change the rowname
s in a tibble
outputted by a map_dfr
looping function from the purrr
package below without success. Is there a fix?
foo <- function(x){
total <- rbinom(1, x, .5)
fixed <- total - 2
random <- fixed + 3
c(total = total, fixed = fixed, random = random)
}
m <- purrr::map_dfr(.x = c(6:9), .f = foo) # loop and output a tibble (a data.frame)
rownames(m) <- paste0("m", 1:4) # change the rownames
m
BUT rownames
don't change:
# A tibble: 4 x 3
total fixed random
* <dbl> <dbl> <dbl>
1 2 0 3
2 3 1 4
3 4 2 5
4 2 0 3
Upvotes: 3
Views: 11765
Reputation: 887158
It is a tibble
and tibble cannot have a custom row name. An option is to convert to data.frame
and then assign the row names
m <- as.data.frame(m)
rownames(m) <- paste0("m", 1:4)
m
# total fixed random
#m1 3 1 4
#m2 5 3 6
#m3 2 0 3
#m4 5 3 6
If we want to keep a column for identification, the map
, also have .id
which will include either the list
name (if present) or the sequence of the list
purrr::map_dfr(.x = 6:9, .f = foo, .id = 'm')
purrr::map_dfr(.x = setNames(6:9, paste0("m", 1:4)), .f = foo, .id = 'm')
Upvotes: 6