我有一个如下所示的CSV文件:
a, b, c, d
e, f
我想将行旋转看起来像一个矩阵。将每个项目排在第一行,同一行中的所有其他项目排在第一位。
a, b, c, d
b, c, d, a
c, d, a, b
d, a, b, c
e, f
f, e
有关如何在Java或R中实现此功能的建议?
答案 0 :(得分:0)
以下是使用sapply
和子集化的第一行的方法:
vec <- letters[1:4]
sapply(1:4, function(i) vec[c(i:4, 1:i)][1:4])
[,1] [,2] [,3] [,4]
[1,] "a" "b" "c" "d"
[2,] "b" "c" "d" "a"
[3,] "c" "d" "a" "b"
[4,] "d" "a" "b" "c"
R中最简单的方法是使用列表,因为每行中的元素数量不同。使用列表结构,这是一个嵌套的sapply
方法:
myList <- list(letter[1:4], letters[5:6])
sapply(myList, function(j) {
sapply(1:length(j), function(i) {
j[c(i:length(j), 1:i)][1:length(j)]})})
[[1]]
[,1] [,2] [,3] [,4]
[1,] "a" "b" "c" "d"
[2,] "b" "c" "d" "a"
[3,] "c" "d" "a" "b"
[4,] "d" "a" "b" "c"
[[2]]
[,1] [,2]
[1,] "e" "f"
[2,] "f" "e"
答案 1 :(得分:0)
在R
中,我们可以遍历行并使用matrix
来获得预期的输出
lst <- apply(df, 1, function(x) {x1 <-x[nzchar(x)]
head(matrix(x1, nrow=length(x1)+1, ncol = length(x1)),-1)})
lst
#[[1]]
# [,1] [,2] [,3] [,4]
#[1,] "a" "b" "c" "d"
#[2,] "b" "c" "d" "a"
#[3,] "c" "d" "a" "b"
#[4,] "d" "a" "b" "c"
#[[2]]
# [,1] [,2]
#[1,] "e" "f"
#[2,] "f" "e"
注意:如果我们有NA而不是空白(''
),请在上面的代码中使用x1 <- x[!is.na(x)]
。
如果我们需要创建单个数据集,则可以使用rbindlist
中的data.table
library(data.table)
rbindlist(lapply(lst, as.data.frame), fill = TRUE)
# V1 V2 V3 V4
#1: a b c d
#2: b c d a
#3: c d a b
#4: d a b c
#5: e f NA NA
#6: f e NA NA
df <- structure(list(v1 = c("a", "e"), v2 = c("b", "f"), v3 = c("c",
""), v4 = c("d", "")), .Names = c("v1", "v2", "v3", "v4"),
row.names = c(NA, -2L), class = "data.frame")