使用函数

时间:2017-07-01 00:51:12

标签: r function scope data.table multiple-columns

我在尝试使用以下函数获取正确输出时收到错误"Error in myData$ID1 : $ operator is invalid for atomic vectors"。由于某种原因,该函数不会继承列名,因此会收到错误invalid for atomic vectors

那么如何正确地继承colnames以及如何修复此错误?

#HOW TO DO THIS WITH apply below?
myData <- data.table(ID1=c("1;11","2;22","3;33"),ID2=c("a;b","c;d",";"))

readMyRow <- function(myData, myIndex)
{
    #TODO: Error is here because col names not inherited, why?
    s3<- strsplit(myData$`ID1`, split=";")  
    s4<- strsplit(myData$`ID2`, split=";")  
    return(paste(s3[[myIndex]], s4[[myIndex]], sep=";"))
}

#Combine all rows  
myData$Combined <- apply(myData, 1, readMyRow)

正确的输出

myData <- data.table(ID1=c("1;11","2;22","3;33"),ID2=c("a;b","c;d",";"))
s3<- strsplit(myData$`ID1`, split=";")  
s4<- strsplit(myData$`ID2`, split=";")  
paste(s3[[1]], s4[[1]], sep=";")
paste(s3[[2]], s4[[2]], sep=";")
paste(s3[[3]], s4[[3]], sep=";")

1 个答案:

答案 0 :(得分:2)

tl; dr 这些行变成了向量,您需要相应地处理它们。

设定:

myData <- data.table(ID1=c("1;11","2;22","3;33"),ID2=c("a;b","c;d",";"))

readMyRow <- function(myData, myIndex)
{
    #TODO: Error is here because col names not inherited, why?
    s3<- strsplit(myData$`ID1`, split=";")  
    s4<- strsplit(myData$`ID2`, split=";")  
    return(paste(s3[[myIndex]], s4[[myIndex]], sep=";"))
}

如果我们debug(readMyRow)然后apply(myData,1,readMyRow),请进入该函数,然后执行str(),我们会看到该行已缩减为字符向量。

str(myData)
Named chr [1:2] "1;11" "a;b"
- attr(*, "names")= chr [1:2] "ID1" "ID2")

$访问器不适用于原子向量,但[[将:

readMyRow <- function(myData, myIndex=1) {
   s3 <- strsplit(myData[["ID1"]], split=";")  
   s4 <- strsplit(myData[["ID2"]], split=";")  
  return(paste(s3[[myIndex]], s4[[myIndex]], sep=";"))
}

(我还为myIndex添加了一个默认值:否则,如果没有指定索引,该函数将无效。)

apply(myData,1,readMyRow)
     [,1]   [,2]   [,3] 
[1,] "1;a"  "2;c"  "3;" 
[2,] "11;b" "22;d" "33;"