代码之家  ›  专栏  ›  技术社区  ›  anup

使用R中的空数据帧

  •  1
  • anup  · 技术社区  · 8 年前

    我试图定义一个空的df for loop 然后从循环内部填充行/列,如下所示:

    df<- data.frame()
        for (fl in files){
          dt <- read.table(fl, header = FALSE, col.names = c("year","month","value"),
           colClasses = c("character","character","numeric"))
          t <- aggregate(value ~ year, dt, sum)
          df$year <- t$year
          df$value <- t$value * someFunction() 
        }
    

    现在,有多种方法可以在r中创建空df。

    df <- data.frame()
    
    # or another method
    df <- data.frame(Month=character(), 
                     Value=character(), 
                     stringsAsFactors=FALSE) 
    
    # or another method
    df <- data.frame(matrix(nrow = 0, ncol = 2))
    

    但当我给数据帧赋值时,会产生以下错误:

    df$Month <- month.abb
    
    Error in `$<-.data.frame`(`*tmp*`, File, value = c("Jan", "Feb", "Mar",  : 
      replacement has 12 rows, data has 0
    

    我不知道我做错了什么,也不知道我可能有什么误解,但我找不到解决这个问题的方法。有人能给我解释一下吗?

    附笔: df <- data.frame(matrix(nrow = 100, ncol = 2)) 可以,但我不知道这是否是一个好主意,因为我的df将有不同的行数。

    4 回复  |  直到 8 年前
        1
  •  1
  •   moodymudskipper    8 年前

    以下是4种方法来培养你的 data.frame

    col1 <- letters[1:3] # [1] "a" "b" "c"
    col2 <- letters[4:6] # [1] "d" "e" "f"
    

    1-从分配第一列开始

    df1 <- data.frame(col1,stringsAsFactors = FALSE)
    df1$col2 <- col2
    

    l2 <- list()
    l2$col1 <- col1
    l2$col2 <- col2
    df2 <- data.frame(l2,stringsAsFactors = FALSE)
    

    df3 <- data.frame(col1 = character(3), col2 = character(3))
    df3$col1 <- col1
    df3$col2 <- col2
    

    df4 <- data.frame(row.names = 1:3)
    df4$col1 <- col1
    df4$col2 <- col2
    

    检查是否都是等效的:

    identical(df1,df2) # [1] TRUE
    identical(df1,df3) # [1] TRUE
    identical(df1,df4) # [1] TRUE
    
        2
  •  2
  •   fugu    8 年前

    myList <- list()
    
    for (m in 1:length(month.abb)) {
      myList[[m]] <- month.abb[m]
    
    }
    
    df <- as.data.frame(do.call(rbind, myList))
    
        3
  •  2
  •   Len Greski    8 年前

    apply() for()

    Pokémon with stats PokémonData github repository

    我们将用 list.files() lapply() .我们还将使用匿名函数读取数据并执行其他计算。

    是一个数据帧列表,可随后单独处理,或组合成一个单独的数据帧, do.call()

    download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
                  "pokemonData.zip",
                  method="curl",mode="wb")
    unzip("pokemonData.zip")
    
    thePokemonFiles <- list.files("./pokemonData",
                                  full.names=TRUE)    
    pokemonDataFiles <- lapply(thePokemonFiles,function(x) {
         y <- read.csv(x,stringsAsFactors=FALSE)
         y$speedSquared <- y$Speed^2
         y # return data frame to result object
         })
    head(pokemonDataFiles[[1]])
    

    …和输出:

    > head(pokemonDataFiles[[1]])
      Number                  Name Type1  Type2 Total HP Attack Defense SpecialAtk SpecialDef Speed Generation Legendary
    1      1             Bulbasaur Grass Poison   318 45     49      49         65         65    45          1     False
    2      2               Ivysaur Grass Poison   405 60     62      63         80         80    60          1     False
    3      3              Venusaur Grass Poison   525 80     82      83        100        100    80          1     False
    4      3 VenusaurMega Venusaur Grass Poison   625 80    100     123        122        120    80          1     False
    5      4            Charmander  Fire          309 39     52      43         60         50    65          1     False
    6      5            Charmeleon  Fire          405 58     64      58         80         65    80          1     False
      speedSquared
    1         2025
    2         3600
    3         6400
    4         6400
    5         4225
    6         6400
    > 
    

    Forms of the Extract Operator

        4
  •  0
  •   Moh    8 年前

    months = c("Jan","Feb","Mar")
    
    df <- data.frame(Month=character(), 
                 Value=character(), 
                 stringsAsFactors=FALSE)
    
    for (i in 1:length(months)){
    
        df[i,1] = months[i]
    }