在 R 中粘贴 header 和前 2 行并换行
paste header and first 2 rows with a new line in R
我有 3 行
first <- LETTERS[1:9]
second <- c(1:9)
third <- letters[1:9]
> first
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I"
> second
[1] 1 2 3 4 5 6 7 8 9
> third
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i"
我想要带有换行的 9 个元素的输出向量
"A "B
1 2
a" b" and so on
我尝试使用循环,但它不起作用
y <-c()
z <-c()
for(i in 1:length(first)){
y <- paste(first, second, sep = "\n")
z <- paste(y, third, sep = "\n")
}
我出来了
[1] "A\n1\na" "B\n2\nb" "C\n3\nc" "D\n4\nd"
[5] "E\n5\ne" "F\n6\nf" "G\n7\ng" "H\n8\nh"
[9] "I\n9\ni"
这不是给我一个换行符。我的报告的 colnames(data) 格式需要像那样。感谢您的提前。
我对你的理解是否正确,你正在寻找这个:
first <- LETTERS[1:9]
second <- c(1:9)
third <- letters[1:9]
cat(c(first, "\n", second, "\n", third))
A B C D E F G H I
1 2 3 4 5 6 7 8 9
a b c d e f g h i
?
编辑:
这会解决您的问题吗?
Printing R data frame with column names in multiple lines
我有 3 行
first <- LETTERS[1:9]
second <- c(1:9)
third <- letters[1:9]
> first
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I"
> second
[1] 1 2 3 4 5 6 7 8 9
> third
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i"
我想要带有换行的 9 个元素的输出向量
"A "B
1 2
a" b" and so on
我尝试使用循环,但它不起作用
y <-c()
z <-c()
for(i in 1:length(first)){
y <- paste(first, second, sep = "\n")
z <- paste(y, third, sep = "\n")
}
我出来了
[1] "A\n1\na" "B\n2\nb" "C\n3\nc" "D\n4\nd"
[5] "E\n5\ne" "F\n6\nf" "G\n7\ng" "H\n8\nh"
[9] "I\n9\ni"
这不是给我一个换行符。我的报告的 colnames(data) 格式需要像那样。感谢您的提前。
我对你的理解是否正确,你正在寻找这个:
first <- LETTERS[1:9]
second <- c(1:9)
third <- letters[1:9]
cat(c(first, "\n", second, "\n", third))
A B C D E F G H I
1 2 3 4 5 6 7 8 9
a b c d e f g h i
?
编辑: 这会解决您的问题吗?
Printing R data frame with column names in multiple lines