Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert an empty column between every column of a dataframe in R

Tags:

dataframe

r

Say you have a dataframe of four columns:

dat <- data.frame(A = rnorm(5), B = rnorm(5), C = rnorm(5), D = rnorm(5))

And you want to insert an empty column between each of the columns in the dataframe, so that the output is:

           A A1           B B1          C C1           D D1
1 1.15660588 NA  0.78350197 NA -0.2098506 NA  2.07495662 NA
2 0.60107853 NA  0.03517539 NA -0.4119263 NA -0.08155673 NA
3 0.99680981 NA -0.83796981 NA  1.2742644 NA  0.67469277 NA
4 0.09940946 NA -0.89804952 NA  0.3419173 NA -0.95347049 NA
5 0.28270734 NA -0.57175554 NA -0.4889045 NA -0.11473839 NA

How would you do this?

The dataframe I would like to do this operation to has hundreds of columns and so obviously I don't want to type out each column and add them naively like this:

dat$A1 <- NA
dat$B1 <- NA
dat$C1 <- NA
dat$D1 <- NA

dat <- dat[, c("A", "A1", "B", "B1", "C", "C1", "D", "D1")]

Thanks for you help in advance!

like image 473
Luke Avatar asked Jul 09 '15 11:07

Luke


People also ask

How do I add a column between two columns in R?

More specifically, you will learn 1) to add a column using base R (i.e., by using the $-operator and brackets, 2) add a column using the add_column() function (i.e., from tibble), 3) add multiple columns, and 4) to add columns from one dataframe to another.

How do I add a column to a Dataframe in R?

1 Adding new columns. You can add new columns to a dataframe using the $ and assignment <- operators. To do this, just use the df$name notation and assign a new vector of data to it. As you can see, survey has a new column with the name sex with the values we specified earlier.

How do I find empty columns in R?

You can identify the empty columns by comparing the number of rows with empty values with the total number of rows. If both are equal, that the column is empty. You can use the colSums() function to count the empty values in a column.


2 Answers

You can try

 res <- data.frame(dat, dat*NA)[order(rep(names(dat),2))]
 res     
 #           A A.1           B B.1          C C.1           D D.1
 #1 1.15660588  NA  0.78350197  NA -0.2098506  NA  2.07495662  NA
 #2 0.60107853  NA  0.03517539  NA -0.4119263  NA -0.08155673  NA
 #3 0.99680981  NA -0.83796981  NA  1.2742644  NA  0.67469277  NA
 #4 0.09940946  NA -0.89804952  NA  0.3419173  NA -0.95347049  NA
 #5 0.28270734  NA -0.57175554  NA -0.4889045  NA -0.11473839  NA

NOTE: I am leaving the . in the column names as it is a trivial task to remove it.

Or another option is

dat[paste0(names(dat),1)] <- NA
dat[order(names(dat))]
like image 136
akrun Avatar answered Nov 14 '22 21:11

akrun


you can try this

df <- cbind(dat, dat)
df <- df[, sort(names(df))]
df[, seq(2, 8,by=2)] <- NA
names(df) <- sub("\\.", "", names(df))
like image 30
Mamoun Benghezal Avatar answered Nov 14 '22 22:11

Mamoun Benghezal