rnorouzian
rnorouzian

Reputation: 7517

Adding named rows to a data.frame in R

Maybe this is too simple of a question, but I'm trying to add 2 more named rows to data.frame d below without success.

Would you please help me understand what I'm missing, and correcting my approach?

d <- data.frame(ESL = 1:5, prof = 0:4, scope = 2:6, type = 3:7)
rownames(d) <- LETTERS[1:5]
d[6:7,] <- c(com = 0:3, min = 2:5)
d

# DESIRED OUTPUT:
#   ESL prof scope type
# A   1    0     2    3
# B   2    1     3    4
# C   3    2     4    5
# D   4    3     5    6
# E   5    4     6    7
# com 0    1     2    3
# min 2    3     4    5

Upvotes: 2

Views: 3087

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 270348

Use rbind to form the right hand side and specify the rownames on the left hand side.

d[c("com", "min"),] <- rbind(0:3, 2:5)

Upvotes: 5

Iroha
Iroha

Reputation: 34761

You can use rbind() on the named vectors.

rbind(d, com = 0:3, min = 2:5) 

    ESL prof scope type
A     1    0     2    3
B     2    1     3    4
C     3    2     4    5
D     4    3     5    6
E     5    4     6    7
com   0    1     2    3
min   2    3     4    5

Upvotes: 3

Related Questions