2
votes

I am solving a task for my online R course. I have the following vector:

L<-list(Surname=c("Javi","James","Thomas","Arjen","David","Arturo","Robert"),
        Number=c(8,11,25,10,27,23,9),
        Name=c("Martinez","Rodriguez","Mueller","Robben","Alaba","Vidal","Lewandowski")

The task is to output the following result using the paste0()-function:

[1] "8:Javi Martinez" "11:James Rodriguez" "25:Thomas Müller"
[4] "10:Arjen Robben" ...

Using the function:

paste0(L$Nummer,L$Vorname,L$Name)

results in:

[1] "8JaviMartinez"      "11JamesRodriguez"   "25ThomasMueller"  
[4] "10ArjenRobben"

I've tried to use the sep and the collapse arguments, but both of them only affect the end of the elements, for example 8JaviMartinez-, when using sep="-".

1
Use paste and not paste0. There you can specify sep and collapse. Or write paste0(L$Nummer, " ", L$Vorname, " ", L$Name)kath
@Kath is correct. paste0 is a shortcut for paste(x, sep="")Amar
paste0 literally doesn't use a seperator. So try something elseRohit

1 Answers

1
votes

You can try this, I am assuming there is no space before and after colon(:), With the restriction of not using paste, you can choose paste0 with spaces in between.

paste0(L$Number,":",L$Surname," ",L$Name)

However, I am not sure, If there are any further restrictions that are there to solve this problem.

Output:

> paste0(L$Number,":",L$Surname," ",L$Name)
[1] "8:Javi Martinez"      "11:James Rodriguez"  
[3] "25:Thomas Mueller"    "10:Arjen Robben"     
[5] "27:David Alaba"       "23:Arturo Vidal"     
[7] "9:Robert Lewandowski"