0

I am trying to update a single column in each row of data.table structure by either calling a function or running some code inline. However, either I cannot pass a current row to a function or once the data.table is updated all values in the column are identical. This is my code for inline code:

airData <- data.table(Airplane_Data)
airData[, c("NewType") := paste(strsplit(gsub("[^a-zA-Z]", " ", Type), " +")[[1]], collapse = ' ')]

That's how I call function from data.table:

airData[,findReason(toString(Summary))]

I cannot figure out what am I doing wrong

Laurynas Stašys
  • 328
  • 4
  • 16
  • 3
    Is `Airplane_Data` included in some package? – talat Sep 10 '15 at 09:34
  • No, its a csv file that i have imported – Laurynas Stašys Sep 10 '15 at 09:48
  • 1
    Then your question is not reproducible and it'l be difficult to help. Read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and also show the function definition of `findReason` to get helpful answers – talat Sep 10 '15 at 10:28
  • The output of `strsplit` is a `list`. Here you are selecting the first list element by `[[1]]` and then assigning it to `NewType`. Anyway, without a reproducible example, it is not easy.. – akrun Sep 10 '15 at 11:00
  • Maybe use `sep` instead of `collapse`..? I often have to trial-and-error my way through that function. – Frank Sep 10 '15 at 12:54

1 Answers1

0

Too long for a comment.

First, in future you really need to provide as representative sample of your data (or all of it). The code in your question should be reproducible (with the data you provide) and demonstrate the problem. In most cases, just by doing that you'll solve your problem yourself.

This issue generally occurs because the function is returning a vector of length 1, rather than a vector of length nrow(data). For instance:

Type <- LETTERS[1:10]
paste(strsplit(gsub("[^a-zA-Z]", " ", Type), " +")[[1]], collapse = ' ')
# [1] "A"

So here gsub(...) is returning a vector of length 10, strsplit(...) is returning a list of length 10, and then you take the first element of that list and send it to paste(...), which returns a vector of length 1. data.table replicates that value to fill out the column.

I assume findReason(...) does something similar, but of course that's only a guess because you didn't tell us anything about it.

jlhoward
  • 58,004
  • 7
  • 97
  • 140