1

For example, I have three data frames a1, a2, a3 in one list c

a1 <- data.frame(x=c(1,2), y=c(2,3))
a2 <- data.frame(x=c(3,4), y=c(4,5))
a3 <- data.frame(x=c(5,6), y=c(6,7))
c <- list(a1, a2, a3)

Now I have a new data frame b1. I want the a1 in c to be replaced by b1.

b1 <- data.frame(x=c(7,8,9), y=c(8,9,10))

I tried to use c[1] <- b1, which does not work. c <- list(b1, a2, a3) will do what I want, but is there a better way to do it?

R. Schifini
  • 9,085
  • 2
  • 26
  • 32
Code Learner
  • 191
  • 11

1 Answers1

0

You need to use double, rather than single brackets.

a1 <- data.frame(x=c(1,2), y=c(2,3))
a2 <- data.frame(x=c(3,4), y=c(4,5))
a3 <- data.frame(x=c(5,6), y=c(6,7))
c  <- list(a1, a2, a3)  
b1 <- data.frame(x=c(7,8,9), y=c(8,9,10))

c[[1]] <- b1
c
[[1]]
  x  y
1 7  8
2 8  9
3 9 10

[[2]]
  x y
1 3 4
2 4 5

[[3]]
  x y
1 5 6
2 6 7

This thread gives an explanation of bracket usage.

Community
  • 1
  • 1
Hack-R
  • 22,422
  • 14
  • 75
  • 131