-2

I am confused about the paste() function in R.

This is my r code:

paste(cols=list("speed###","dist"), rows=list("speed"))

The ideal output should be:

cols=list("speed###","dist"), rows=list("speed")

But the actual output was:

"speed### speed" "dist speed" 

Can anyone help me to figure out and get the ideal output?

I will appreciate any reply here!

Thank you!

Best regards!

Joanna
  • 663
  • 7
  • 21
  • 2
    I guess you need `c` instead of `paste`. Do you need `c(cols=list("speed###","dist"), rows=list("speed"))` – akrun Dec 05 '16 at 16:03
  • 1
    Or `list` instead of `paste`. The 'ideal output' should be stated in terms of an actual R object. – Pierre L Dec 05 '16 at 16:06
  • Hi, @akrun. Thanks for your reply. I think `c` is to separate the "speed###", "dist" and "speed" into `$col1`, `$col2` and `$rows`but it didn't get the exactly the same output as `cols=list("speed###","dist"), rows=list("speed")`, am I missed some steps? – Joanna Dec 05 '16 at 16:10
  • I am not following your structure. Please use a dput to show what you intended, a list of lists or a single list with 3 elements? – akrun Dec 05 '16 at 16:14
  • You may not need a `list` at all. R lists are not like other programming languages you may have used. – Pierre L Dec 05 '16 at 16:19
  • Thank you so much for all of the replies! I think I mess up R with different languages! :) – Joanna Dec 05 '16 at 17:16

1 Answers1

2

What I think is happening with your code is:

  1. You are passing list("speed###","dist") and list("speed") as parameters cols and rows, not as strings, and paste ignores names of parameters. Also, lists are converted to characters:c("speed###","dist"), c("speed")
  2. Since parameters don't have the same length, the second is replicated (i.e. c("speed###","dist") and c("speed", "speed")).
  3. Then, first elements are pasted together, and second elements are as well, returning a vector with each pasted string: c("speed### speed", "dist speed")

Why do you need what you are asking for? I mean, you plan to do.call(matrix, list(cols=c("speed###","dist"), rows=c("speed"))) or something?

Besides, you should use c instead of list. I don't understand the need of using list here.

Gere Caste
  • 130
  • 5