我可以访问apply()

时间:2015-10-16 19:33:14

标签: r apply

我们需要填写分类数据表。我倾向于为循环编写太多,我试图弄清楚如何用apply()来做。我正在扫描最后一列以查找非缺失值,然后在每列中填充其上方的值,仅在对角线上。因此,如果有3列,则会填充最后一列的值。我会为每个“更高的分类水平”或左下一列重复它:

# fills in for Family-level taxonomy
for(i in nrows(DataFrame)){  
  if(is.na(DataFrame[[4]][i])) next
    else {
      DataFrame[[3]][i] <- DataFrame[[3]][i-1]
      DataFrame[[2]][i] <- DataFrame[[2]][i-2]
      DataFrame[[1]][i] <- DataFrame[[1]][i-3]
     }
}

# Repeat to fill in Order's higher taxonomy (Phylum and Class)
for(i in nrows(DataFrame)){  # fills in for Family
  if(is.na(DataFrame[[3]][i])) next
    else {
      DataFrame[[2]][i] <- DataFrame[[2]][i-2]
      DataFrame[[1]][i] <- DataFrame[[1]][i-3]
     }
}
# And again for each column to the left.

数据可能如下所示:

Phylum     Class       Order        Family  
Annelida   
           Polychaeta  
                       Eunicida
                                    Oenoidae
                                    Onuphidae     
                       Oweniida
                                    Oweniidae

然后,将对该订单中的每个唯一系列以及类中的每个唯一订单和门中的每个唯一类重复此操作。基本上,我们需要从每个非缺失值的上方的下一个非缺失值填充左侧的值。所以最终的结果是:

Phylum     Class       Order    Family  
Annelida   
Annelida  Polychaeta  
Annelida  Polychaeta  Eunicida
Annelida  Polychaeta  Eunicida Oenoidae
Annelida  Polychaeta  Eunicida Onuphidae     
Annelida  Polychaeta  Oweniida
Annelida  Polychaeta  Oweniida Oweniidae

我们不能只复制列,因为一旦我们达到新的门级别,用一个缺失值复制类停止,订单可能有两个缺失值等等...
我想挑战是我需要Dataframe [[j]] [i-n]的值,无论我传递什么函数来应用。当apply将'x'传递给函数时,它是否传递了一个带有属性的对象(比如索引/行名)或者只传递了值?

或者这是一个浪费的想法,用for循环做,如果我真的需要速度,使用rcpp。这是每年完成的数据帧有大约8,000行和13列我们操作。我不认为表演会成为一个问题...但我们还没有尝试过。不知道为什么。

3 个答案:

答案 0 :(得分:2)

这是我的方法,只要您的数据看起来像我在猜测:

library(tidyr)
library(dplyr)
data[data == ""] <- NA
data %>% fill(-Family) %>%
         filter(!is.na(Family)) 

输出:

    Phylum      Class    Order    Family
1 Annelida Polychaeta Eunicida  Oenoidae
2 Annelida Polychaeta Eunicida Onuphidae
3 Annelida Polychaeta Oweniida Oweniidae

如果你想要空行,你可以尝试这个,这允许任意嵌套和取消:

data %>% fill(-Family) %>%
  filter(!is.na(Family)) %>%
  do(plyr::rbind.fill(unlist(lapply(1:nrow(.), function(z) lapply(1:4, function(xx) .[z,][1:xx])), recursive = FALSE))) %>%
  distinct()

     Phylum      Class    Order    Family
1  Annelida       <NA>     <NA>      <NA>
2  Annelida Polychaeta     <NA>      <NA>
3  Annelida Polychaeta Eunicida      <NA>
4  Annelida Polychaeta Eunicida  Oenoidae
5  Annelida Polychaeta Eunicida Onuphidae
6  Annelida Polychaeta Oweniida      <NA>
7  Annelida Polychaeta Oweniida Oweniidae
8  Annelida       blah     <NA>      <NA>
9  Annelida       blah     blah      <NA>
10 Annelida       blah     blah      blah

数据输入:

structure(list(Phylum = c("Annelida", NA, NA, NA, NA, NA, NA, 
NA, NA, NA), Class = c(NA, "Polychaeta", NA, NA, NA, NA, NA, 
"blah", NA, NA), Order = c(NA, NA, "Eunicida", NA, NA, "Oweniida", 
NA, NA, "blah", NA), Family = c(NA, NA, NA, "Oenoidae", "Onuphidae", 
NA, "Oweniidae", NA, NA, "blah")), .Names = c("Phylum", "Class", 
"Order", "Family"), row.names = c(NA, -10L), class = "data.frame")

答案 1 :(得分:1)

作为其他解决方案的替代方案,您还可以使用na.locf包中的zoo函数替换NA - 值与上次观察值( locf = last观察结转)。

# replace empty spaces with NA values
df[df == ""] <- NA

# use na.locf to replace the NA values    
library(zoo)
df <- na.locf(df)

这导致:

> df
    Phylum      Class    Order    Family
1 Annelida       <NA>     <NA>      <NA>
2 Annelida Polychaeta     <NA>      <NA>
3 Annelida Polychaeta Eunicida      <NA>
4 Annelida Polychaeta Eunicida  Oenoidae
5 Annelida Polychaeta Eunicida Onuphidae
6 Annelida Polychaeta Oweniida Onuphidae
7 Annelida Polychaeta Oweniida Oweniidae

使用过的数据:

df <- structure(list(Phylum = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Annelida"), class = "factor"), 
                     Class = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Polychaeta"), class = "factor"), 
                     Order = structure(c(1L, 1L, 2L, 1L, 1L, 3L, 1L), .Label = c("", "Eunicida", "Oweniida"), class = "factor"), 
                     Family = structure(c(1L, 1L, 1L, 2L, 3L, 1L, 4L), .Label = c("", "Oenoidae", "Onuphidae", "Oweniidae"), class = "factor")), 
                .Names = c("Phylum", "Class", "Order", "Family"), class = "data.frame", row.names = c(NA, -7L))

答案 2 :(得分:0)

这是一种方式:

x <- matrix(rnorm(100), 10,10)
x <- cbind(1:nrow(x), x)

output <- apply(x, 1, function(i) {
  rowID <- as.numeric(i[1])
  x_orig <- unlist(i[-1])
  ## ... do some more stuff
  return(...something...)
})