-17

I need to be able to apply the following function to the all of the columns in the data frame:

data2$Zones <- vapply(data2$Zones, paste, collapse = ", ", character(1L))

how can I do this?

user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 6
    Please add `data2` to the post or at least use `dput()` in order to reproduce the issue! – Duck Sep 24 '20 at 13:51
  • 2
    `vapply(dat, paste, collapse=", ", character(1L))` or `vapply(dat, toString, character(1L))` – jay.sf Sep 24 '20 at 14:08
  • @Duck, I cannot add dput, it is confidential data. I think the problem is that cell values have "," in them. – user1471980 Sep 28 '20 at 17:59
  • 11
    You don't need to input confidential data, but you could generate a structurally similar sample that people can run to see what exactly is the problem. – Diego Queiroz Sep 29 '20 at 13:44
  • All of the columns are list columns? – s_baldur Sep 29 '20 at 13:56
  • 5
    Even if you can't post the data itself, you can create a [reproducible example](https://stackoverflow.com/q/5963269/5325862) with mockup data that recreates the issue. You're saying you think it's because of a comma in cell values, but we have no way of knowing what that means exactly or how to help solve it – camille Oct 04 '20 at 22:56

3 Answers3

1

Based on your description:

data2[] <- lapply(data2, function(x) vapply(x, paste, collapse = ", ", character(1L)))

Or

data2[] <- lapply(data2, function(x) vapply(x, toString, character(1L)))
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • this kinda worked but I have another issue. when I do this, One of the cell becomes data frame like this: c("CONTEXTLESS", "CONTEXTLESS", "CONTEXTLESS", "CONTEXTLESS", "CONTEXTLESS", "CONTEXTLESS", "CONTEXTLESS", "CONTEXTLESS"), c("CLIENT", "ORG", "PRIMARY", "DC", "app", "OS_VERSION", "SERVER_TYPE", "TIER"), c("NO", "CORP", "test", "test1", NA, "Red Hat Enterprise Linux", "test3", "test4"). I need to remove the first column that says contexttless and second column be column name and 3rd column be the entry. Any ideas? – user1471980 Oct 20 '20 at 17:07
  • Maybe best to open a new question. Just make sure to have reproducible input data and be clear about the expected output. – s_baldur Oct 21 '20 at 08:00
1

I would do using data.table

Here is an example code:

library(data.table)
setDT(data2)
collist <- c("col1", "col2", "col3") # whatever columns you have in data2
newcols <- c("col1new", "col2new", "col3new") # the new columns
data2[, (newcols) := lapply(.SD, function(x)  paste(x, collapse = ", "), .SDcols = collist]

In case you want to apply the function on some columns, e.g., paste some columns and create a new column:

data2[, newColumn := paste(col1, col2, col3, collapse = ", ")]
maop
  • 194
  • 14
1

Are you trying to add a ", 1" to each cell in each column?

library(purrr)
df <- tibble(x = c("a", "b"),
             y = c("c", "d"),
             z = c("e", "f"))

df
# A tibble: 2 x 3
#   x     y     z    
#   <chr> <chr> <chr>
# 1 a     c     e    
# 2 b     d     f   

df2 <- map_dfc(df, ~ paste(.x, sep = ", ", character = 1L))

df2
# A tibble: 2 x 3
#   x     y     z    
#   <chr> <chr> <chr>
# 1 a, 1  c, 1  e, 1 
# 2 b, 1  d, 1  f, 1 
ciakovx
  • 334
  • 1
  • 5