Create an empty data.frame

前端 未结 17 1140
猫巷女王i
猫巷女王i 2020-11-22 16:06

I\'m trying to initialize a data.frame without any rows. Basically, I want to specify the data types for each column and name them, but not have any rows created as a result

17条回答
  •  广开言路
    2020-11-22 16:14

    If you want to create an empty data.frame with dynamic names (colnames in a variable), this can help:

    names <- c("v","u","w")
    df <- data.frame()
    for (k in names) df[[k]]<-as.numeric()
    

    You can change the types as well if you need so. like:

    names <- c("u", "v")
    df <- data.frame()
    df[[names[1]]] <- as.numeric()
    df[[names[2]]] <- as.character()
    

提交回复
热议问题