Skip to content
Prev 170826 / 398503 Next

:How to insert a column in a data frame

On 18/02/2009, at 12:51 PM, Laura Rodriguez Murillo wrote:

            
(1) y <- cbind(x[,1:6],clyde=rep(1,300),x[,7:ncol(x)])

(2) y <- as.data.frame(c(x[1:6],list(clyde=rep(1,300)),x[7:length(x)]))

where x is your original data stored as a matrix or data frame in  
method (1),
and as a data frame in method (2), and y is the new data set with the  
extra
column of 1's.

Or, if you really are just duplicating column 6:

	y <- x[,c(1:6,6,7:ncol(x))]
	names(y)[7] <- "clyde"        # For consistency with other methods.

These work with your toy example.  I haven't tried them out with data  
sets of
the size of your real data set.

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}