在數據幀創建新的列我有一個DF,如下所示:錯誤中的R
id type start end features
1 5 word 1 2 NN
2 6 word 3 3 .
3 7 word 5 12 NN
4 8 word 14 19 VBZ
5 9 word 21 30 NN
6 10 word 32 32 WDT
7 11 word 34 37 VBP
8 12 word 39 41 IN
9 13 word 43 44 IN
10 14 word 46 46 DT
我想創建一個新的列「總和」與「開始」和「結束」的每個值的總和。
我創建了以下功能:
mySum <- function(row) {
row["start"]+row["end"]
}
df$sum <- apply(df,1, mySum);
但是當我運行此我得到以下錯誤:
Error in row["start"] + row["end"] :
non-numeric argument to binary operator
但如果我只保留行[「開始」]或行[」結束「],它會被創建。
我也試圖強制列中的每個值都是數字。
df$start = as.integer(as.vector(df$start));
df$end = as.integer(as.vector(df$end));
但我仍然得到相同的錯誤,只有當我添加的值。
我的數據幀的結構如下: 後,我跑了dput(droplevels(head(df,10)))
structure(list(id = 5:14, type = c("word", "word", "word", "word",
"word", "word", "word", "word", "word", "word"), start = c(1L,
3L, 5L, 14L, 21L, 32L, 34L, 39L, 43L, 46L), end = c(2L, 3L, 12L,
19L, 30L, 32L, 37L, 41L, 44L, 46L), features = list(structure(list(
POS = "NN"), .Names = "POS"), structure(list(POS = "."), .Names = "POS"),
structure(list(POS = "NN"), .Names = "POS"), structure(list(
POS = "VBZ"), .Names = "POS"), structure(list(POS = "NN"), .Names = "POS"),
structure(list(POS = "WDT"), .Names = "POS"), structure(list(
POS = "VBP"), .Names = "POS"), structure(list(POS = "IN"), .Names = "POS"),
structure(list(POS = "IN"), .Names = "POS"), structure(list(
POS = "DT"), .Names = "POS"))), .Names = c("id", "type",
"start", "end", "features"), row.names = c(NA, 10L), class = "data.frame")
需要注意的是,所採取的所有鑄造的問題分開,如果你用你的錯誤很可能會發生變化: 'mySum < - 功能(行){ as.integer(行[ 「開始」])+ as.integer (行[[「end」]]) }' 根據定義,'row'不能是你認爲它的向量。 – Vongo