Jaafa
Jaafa

Reputation: 141

How to select rows in one column and convert into new table as columns?

I am importing data from an excel file into Rstudio and want to select specific rows within one column to create two new columns in a new table.

For example I have a column as such:

Old1

1

2

3

5

6

7

40

8

12

12

12

6

And I want to select rows 2-5 and rows 8-12 to create two new separate columns in a new table. What would be the best library or function to use for this?

So in this example the resulting output would be like this:

enter image description here

Upvotes: 3

Views: 153

Answers (1)

akrun
akrun

Reputation: 887203

Here is one option with cbind.fill from rowr

library(rowr)
out <- cbind.fill(df1$no1408[2:5], df1$no1408[8:12], fill = NA)
names(out) <- paste0("New_", 1:2)

data

df1 <- structure(list(no1408 = c(10L, 2L, 3L, 5L, 6L, 8L, 20L, 40L, 
 8L, 12L, 12L, 6L)), class = "data.frame", row.names = c(NA, -12L
 ))

Upvotes: 3

Related Questions