如何选择一个列中的行,并将其转换成新表中的列?

3
我将从Excel文件导入数据到Rstudio,并希望选择一个列中的特定行,以创建一个新表中的两个新列。
例如,我有一个如下所示的列:
Old1

1

2

3

5

6

7

40

8

12

6

我希望选择第2-5行和第8-12行,创建两个新的独立列放入一个新表格中。哪个库或函数是最好的选择?

在这个例子中,输出结果应该像这样:

enter image description here


尝试使用 rowr::cbind.fill(df1$no1408[2:5], df1$no1408[8:12]) - akrun
1个回答

3

这里有一个使用rowr中的cbind.fill函数的选项。

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

数据

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
 ))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接