1

I want to create a vector or a list in a dataset concatenating the scores of several columns in another dataset.

I can do it like this:

my_vec <- c(x$v1, x$v2, x$v3...)

But I would need like 60 lines of code. I am pretty sure there is another way of doing it. When I try this:

my_vec <- c(x$v1:x$v644)

I get this error message

Warning messages:
1: In t$`1`:t$`644` :
  numerical expression has 20 elements: only the first used

My dataset looks like this

x <- read.table(
  text = "  v1   v2  V3   
  0      1     0
  1      0     1
  0      0     0
  0      0     1
  1      0     0",
  header = TRUE
)

And as an output I would like just a vector with values for each column one after the other, like this:

my_vec <- c(0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0)
Nate
  • 10,361
  • 3
  • 33
  • 40
Anita-
  • 21
  • 7

3 Answers3

0

You can use paste(). If they are all in the same data.frame, you can create a new column with the concatenation.

require(tidyverse)

df %>% 
  mutate(conc = paste(a, b, c))

Output:

  a b c  conc
1 a e g a e g
2 b f h b f h
3 c g i c g i
4 d h j d h j

Sample data:

df <- data.frame(a = letters[1:4], 
                 b = letters[5:8], 
                 c = letters[7:10])

Edit

If they are in diffrent vectors, you can do something like this:

a <- letters[1:4]
b <- letters[5:8] 
c <- letters[7:10]

reduce(list(a, b, c), union)

Output:

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
DJV
  • 4,743
  • 3
  • 19
  • 34
  • Thank you! They are not in the same dataset. And I do not want another column, I want it to be like a list in a separate object. In you example, the output that I would like to get would be a list containing: a, b, c, d, e, f, g, h, g, h, i, j, etc. I am sorry if I haven't explained myself correctly... – Anita- Mar 12 '19 at 16:51
  • No worries, please see my edit. Is that what you meant? – DJV Mar 12 '19 at 17:00
  • I have the same warning message when your using paste... Do I need to name all the columns one by one? Isn't there a cleaner way of doing it? THANKS – Anita- Mar 12 '19 at 17:01
  • Nope, still not working... =( Same warning. I am doing this df<-mutate(conc = paste(x$v1:xv2)) ##to avoid putting all numbers up to 644 – Anita- Mar 12 '19 at 17:04
  • what warning do you get? – DJV Mar 12 '19 at 17:04
  • Error in mutate_(.data, .dots = compat_as_lazy_dots(...)) : argument ".data" is missing, with no default In addition: Warning messages: 1: In t$`1`:t$`644` : numerical expression has 20 elements: only the first used – Anita- Mar 12 '19 at 17:05
  • Thanks for your edit!! The error is stil in the same part. I need a way to replace "(a,b,c)" by "(a:c)" without getting that warning... Any idea? – Anita- Mar 12 '19 at 17:09
  • Sorry, I was late on your edits, hehe. That is better! THANKS! But I still makes another dataset, rather than lists. And in any case, it makes me do at least thirty lines of code... Isn't there a more proper way of doing it? – Anita- Mar 12 '19 at 17:11
  • How would you like to specify your input vectors? are they randomly named? are they the same? – DJV Mar 12 '19 at 17:12
  • No, they are not random. They come from a dataset that has scores in it. The scores are 0s and 1s. So I need to pick the 0s and 1s in each column and put them together in a string/a list/a vector... – Anita- Mar 12 '19 at 17:13
  • Are they all part of `data.frame` x? where each column is your vector to concatenate? – DJV Mar 12 '19 at 17:19
0

could a simple solution like

my_vec = x[, 1:644]

work?

NM_
  • 1,887
  • 3
  • 12
  • 27
0

Assuming that x is a data.frame:

> as.vector(unlist( split(as.matrix(x), row(x)) ))

The output might be a vector of characters, however you can make the values numeric again using as.numeric().

Credit:

Got the idea using the following post as a guide

How to split a R data frame into vectors (unbind)

EDIT:

Using your example data, we get the following:

x <- read.table(
  text = "  v1   v2  V3   
  0      1     0
  1      0     1
  0      0     0
  0      0     1
  1      0     0",
  header = TRUE
)

> as.vector(unlist( split(as.matrix(x), row(x)) ))
[1] 0 1 0 1 0 1 0 0 0 0 0 1 1 0 0
NM_
  • 1,887
  • 3
  • 12
  • 27
  • I will try this. I could solve it by using a double loop in Excel but I still want to do it through R. Thanks! – Anita- Mar 13 '19 at 17:08