0

As expected, this code

myLines="
Greek
alpha
beta
gamma
"
myData <- read.table(text=myLines, header=TRUE)
print(myData)

produces:

  Greek
1 alpha
2  beta
3 gamma

I now want to mutate the vector c(1,2,3,4) to the end of each row in order to get this:

  Greek justSomeVector
1 alpha     c(1,2,3,4)
2  beta     c(1,2,3,4)
3 gamma     c(1,2,3,4)

So I tried this:

library(dplyr)
myData=myData%>%mutate(justSomeVector=c(1,2,3,4))

And this yields this error:

Error in mutate_impl(.data, dots) : 
  Column `justSomeVector` must be length 3 (the number of rows) or one, not 4

How do I have to change my code in order to make the mutation work?

nexonvantec
  • 572
  • 1
  • 5
  • 18
  • Possible duplicate of https://stackoverflow.com/questions/9547518/create-a-data-frame-where-a-column-is-a-list ? – zx8754 May 23 '18 at 08:59

1 Answers1

1

You need to list the list of numbers, or else it will mutate as if the justSomeVector is a vector that needs to be spread along the rows

myData=myData%>%mutate(justSomeVector=list(c(1,2,3,4)))
#  Greek justSomeVector
#1 alpha     1, 2, 3, 4
#2  beta     1, 2, 3, 4
#3 gamma     1, 2, 3, 4
Shique
  • 724
  • 3
  • 18