0
我有以下设置我想要剪切文本字符串,产生一个列表,然后重新指定为data.table作为嵌套列。它适用于n> 1行,但在n = 1行时不适用。我能做些什么来确保一致性:data.table一致性分配参考n = 1行vs n> 1
library(data.table)
cutdash <- function(x) {strsplit(x, '-')}
## n > 1 row
x <- data.frame(
text = c("I paid for books-He was not interesting in his teaching.", 'good'),
id = 1:2, stringsAsFactors = FALSE)
## n = 1 row
y <- data.frame(text = "I paid for books-He was not interesting in his teaching.",
id = 3, stringsAsFactors = FALSE)
## good
x2 <- data.table::data.table(x)
x2[, text := cutdash(text)][]
## text id
## 1: I paid for books,He was not interesting in his teaching. 1
## 2: good 2
## bad
y2 <- data.table::data.table(y)
y2[, text := cutdash(text)][]
## text id
## 1: I paid for books 3
## Warning message:
## In `[.data.table`(y2, , `:=`(text, cutdash(text))) :
## Supplied 2 items to be assigned to 1 items of column 'text' (1 unused)
不是说第二个数据帧有1行只允许第一个元素被重新分配。