How to add a new column to a data frame?

3

Hello, does anyone know how to add a new column to a data frame?

DF <- data.frame (predictor=c ("a", "a", "b", "b"), respuesta=c (13.5, 0, 2.6, 4.1))
    
asked by FlorLimno 22.11.2018 в 20:18
source

1 answer

3

There are several ways to add new columns. Some of them are:

Add them when creating the data.frame :

DF <- data.frame (predictor=c ("a", "a", "b", "b"), respuesta=c (13.5, 0, 2.6, 4.1),
                  ColNueva=rnorm(4))

If the object is already created:

DF[,3] <- rnorm(4)
DF$ColNueva<- rnorm(4)
DF<- data.frame(DF, ColNueva=rnorm(4))
cbind(DF, ColNueva=rnorm(4))

For a future question, you should specify which output you would like.

    
answered by 22.11.2018 в 20:26