简体   繁体   中英

Reassigning values to columns in ffdf [R]

I am having trouble doing the following operations in a larger dataset. I wonder if there is a built in way to do it with either ff or ffdf.

Example: Modifying a character columns in an ffdf object using substr and reassign it as a different column:

require(ffbase)
> iris
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa

#Convert to ff object
A <- as.ffdf(iris)

I can access the "Species" column

A$species
> A$Species
ff (open) integer length=150 (150) levels: setosa versicolor virginica
      [1]       [2]       [3]       [4]       [5]       [6]       [7]       [8]               [143]     [144] 
setosa    setosa    setosa    setosa    setosa    setosa    setosa    setosa            : virginica virginica 
    [145]     [146]     [147]     [148]     [149]     [150] 
virginica virginica virginica virginica virginica virginica

But if I want to substring the characters 1 through 3, for example, i get the following error:

> substr(as.character(A$Species),1,3)
Error in substr(as.character(A$Species), 1, 3) : 
  extracting substrings from a non-character object 

What guidelines are there to modifying columns in an ffdf object?

Edit

I also tried the ffdfdply approach. It seems to take really long for a reasonably small data:

substrff <- function(x){
  x$new <- substr(x$Species,1,8)
  return(x)
}

B <-  ffdfdply(x=A, split = A$Species, FUN = substrff)
require(ffbase)
data(iris, package = "datasets")
x <- as.ffdf(iris)
x$spec <- with(x[c("Species")], substr(Species, 1, 4))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM