简体   繁体   English

如何创建具有定义的行数的data.frame?

[英]How to create data.frame with defined number of rows?

I want to create a data frame and I know how many rows it is going to have in advance. 我想创建一个数据框,我知道它要提前多少行。 Following code creates "empty" data frame: 以下代码创建“空”数据框:

result.data.frame <- data.frame(TrusterID = integer(),
                                TrusteeID = integer(),
                                RTT = integer(),
                                RTD = integer(),
                                RDT = integer(),
                                RDD = integer(),
                                TrustValue = factor(levels = c("1", "-1", "0")))

Now how to create data frame like that with 10 rows and initial values: 0 for integers and "0" for factor? 现在如何创建具有10行和初始值的数据框:0代表整数,“ 0”代表因数?

Just initialise the data frame as you have already, and then do 只需初始化数据帧,然后执行

result.data.frame[1:10, ] <- 0

The result of this will be 其结果将是

> str(result.data.frame)
'data.frame':   10 obs. of  7 variables:
 $ TrusterID : num  0 0 0 0 0 0 0 0 0 0
 $ TrusteeID : num  0 0 0 0 0 0 0 0 0 0
 $ RTT       : num  0 0 0 0 0 0 0 0 0 0
 $ RTD       : num  0 0 0 0 0 0 0 0 0 0
 $ RDT       : num  0 0 0 0 0 0 0 0 0 0
 $ RDD       : num  0 0 0 0 0 0 0 0 0 0
 $ TrustValue: Factor w/ 3 levels "1","-1","0": 3 3 3 3 3 3 3 3 3 3

You can use initialize the values as follows: 您可以按以下方式使用初始化值:

val <- as.integer (rep(0,10))

result.data.frame <- data.frame(TrusterID = val,
                                TrusteeID = val,
                                RTT = val,
                                RTD = val,
                                RDT = val,
                                RDD = val,
                                TrustValue = factor(as.factor(rep(0,10)), 
                                                    levels = c("1", "-1", "0")))


> str(result.data.frame)
'data.frame':   10 obs. of  7 variables:
 $ TrusterID : int  0 0 0 0 0 0 0 0 0 0
 $ TrusteeID : int  0 0 0 0 0 0 0 0 0 0
 $ RTT       : int  0 0 0 0 0 0 0 0 0 0
 $ RTD       : int  0 0 0 0 0 0 0 0 0 0
 $ RDT       : int  0 0 0 0 0 0 0 0 0 0
 $ RDD       : int  0 0 0 0 0 0 0 0 0 0
 $ TrustValue: Factor w/ 3 levels "1","-1","0": 3 3 3 3 3 3 3 3 3 3

You could also do: 您也可以这样做:

n <- 10
result.data.frame <- data.frame(TrusterID = integer(n),
                                TrusteeID = integer(n),
                                RTT = integer(n),
                                RTD = integer(n),
                                RDT = integer(n),
                                RDD = integer(n),
                                TrustValue = replace(factor(levels = c("1", "-1", "0"))[1:n],1:n,"0"))

which will give you: 这将为您提供:

   # TrusterID TrusteeID RTT RTD RDT RDD TrustValue
# 1          0         0   0   0   0   0          0
# 2          0         0   0   0   0   0          0
# 3          0         0   0   0   0   0          0
# 4          0         0   0   0   0   0          0
# 5          0         0   0   0   0   0          0
# 6          0         0   0   0   0   0          0
# 7          0         0   0   0   0   0          0
# 8          0         0   0   0   0   0          0
# 9          0         0   0   0   0   0          0
# 10         0         0   0   0   0   0          0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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