-1

I have 3 rows

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"

I want the output vector of 9 elements with new line like that

"A    "B
 1     2
 a"    b"  and so on

I try to use loop but it doesn't work

y <-c()
z <-c()

 for(i in 1:length(first)){
  y <- paste(first, second, sep = "\n")
  z <- paste(y, third, sep = "\n")
}

I got out put

[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"

This is not give me a newline. The format of my colnames(data) for report need to be like that. Thanks for advance.

BIN
  • 781
  • 2
  • 10
  • 24
  • the first element will be A newline 1 and newline a, sep="\n", not give me newline – BIN Jul 22 '16 at 16:29
  • this is out put [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" – BIN Jul 22 '16 at 16:30

1 Answers1

2

Am I understanding you correctly that you are looking for this:

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

?

EDIT: Will this solve your problem?

Printing R data frame with column names in multiple lines

Community
  • 1
  • 1
elmo
  • 325
  • 1
  • 12
  • yes, c(first, "\n", second, "\n", third) is a vector – elmo Jul 22 '16 at 17:32
  • it's not a vector, it doen't have "" for each, I can't get the first element – BIN Jul 22 '16 at 17:34
  • c(first, "\n", second, "\n", third)[1] returns: "A". It is a vector. – elmo Jul 22 '16 at 17:35
  • I try to add it with data frame, I have z <-c(1:9), I rbind it to z – BIN Jul 22 '16 at 17:37
  • z has length 9, c(first, "\n", second, "\n", third) has length 29, why would you want to rbind them? Let a <- c(first, "\n", second, "\n", third). Maybe you want to cat(c(a, "\n", z)). If not, I have no idea what you want to do. – elmo Jul 22 '16 at 17:43
  • I need to make cat() is colnames(data) with 9 columns, cat() give me a string. The first colnames is "A\n1\a" look like your output, the second colname(data) is "B\n2\b" – BIN Jul 22 '16 at 17:52
  • I think I finally understood your question. I edited my answer and added a link to another question already asked. Hope this helps. – elmo Jul 22 '16 at 18:03