【发布时间】:2019-03-05 17:46:10
【问题描述】:
我的问题有点棘手。我有一个向量如下
vec <-c("Camera","Battery","Protection")
我有如下数据框 Camera_pos # 一个包含一些列的数据框(我们可以忽略这里的细节)。同样明智的是,我们还有其他数据框,例如 Camera_neg、Battery_pos、Battery_neg、Protection_pos、Protection_neg
所以我有 6 个数据框,其中包含一些观察结果,而这些细节与问题无关。
我正在尝试构建一个从向量和数据帧中提取数据/值的新数据帧。
df <- data.frame(Features = character(),Positive = numeric(), Negative = numeric()) # empty data frame
for(i in 1:length(vec)){
df$Features[i] = vec[i] # Camera in case of vec[1]
df$Positive[i] = nrow() # not sure what code to write here, but this code should call the nrow() of Camera_pos ( i =1 is considered here)
df$Negative[i] = nrow() # not sure what code to write here, but this code should call the nrow() of Camera_neg
}
代码应该有点像nrow(vec[i]_pos) 即nrow(Camera_pos) 在 i =1 的情况下。请求您对此提供帮助
P.S : 同样,该函数也应该能够调用其他向量中的元素,因此 df 填充了 3 行和 3 列
输出应该如下所示
Features Positive Negative
Camera 3 3
Battery 3 3
Protection 3 3
【问题讨论】:
-
你好@Arun你能提供一个可重现的例子吗?我推荐你去this问题了解细节?如果我们有数据框可以玩会更容易
-
嗨,德里克,感谢您的回复。我已经用更多细节编辑了这个问题。如果您需要更多详细信息,请回复。谢谢
标签: r function dataframe data-mining