如何使用 R 调用数据帧并在函数中查找 nrow?



我的问题有点棘手。我有一个向量如下

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)即在 i = 1 的情况下nrow(Camera_pos)。请您帮忙

P.S :同样,该函数也应该能够调用其他向量中的元素,因此 df 填充了 3 行和 3 列

输出应如下所示

Features        Positive         Negative
Camera          3                3
Battery         3                3
Protection      3                3

这将是一种方法:

#This would name all the files you have in your working directory
files <- ls()
library(stringr)
df <- data.frame(Features = rep(NA, length(vec)),Positive = rep(NA, length(vec)), Negative = rep(NA, length(vec))) # empty data frame
for(i in 1:length(vec)){
df$Features[i] = vec[i] # Camera in case of vec[1]
#Get a temp with only the name of vec[i] of your data.frame
temp <- files[str_detect(files, vec[i])]
df$Positive[i] = nrow(get(temp[str_detect(temp, "pos")])) # 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(get(temp[str_detect(temp, "neg")])) # not sure what code to write here, but this code should call the nrow() of Camera_neg
}

如果你有什么不明白的地方,我可以更详细地解释

这是一个tidyverse的方法

Camera_pos <- data.frame(Text = c("text1","text2","text3"), Score = c(1.45,6.78,6.879))
Camera_neg <- data.frame(Text = c("text1","text2","text3"), Score = c(-0.5,-1.8,-1.4))
Battery_pos <- data.frame(Text = c("text1","text2","text3"), Score = c(0.5,1.8,1.4))
Battery_neg <- data.frame(Text = c("text1","text2","text3"), Score = c(-0.5,-1.8,-1.4))
Protection_pos <- data.frame(Text = c("text1","text2","text3"), Score = c(0.5,1.8,1.4))
Protection_neg <- data.frame(Text = c("text1","text2","text3"), Score = c(-0.5,-1.8,-1.4))
vec <-c("Camera","Battery","Protection")
library(tidyverse)
# get all your environment objetcs
obj_names = ls()
# function the returns the names of your workspace objects that match a pattern
f = function(x) data.frame(x, obj_names = obj_names[grepl(x, obj_names)], stringsAsFactors = F)
map_df(vec, ~f(.x)) %>%                       # apply the function to each pattern
mutate(d = map(obj_names, ~get(.x))) %>%    # get the datasets
unnest() %>%                                # unnest data
mutate(type = ifelse(Score > 0, "Positive", "Negative")) %>%  # get the type of each score
count(x, type) %>%                          # count combinations
spread(type, n)                             # reshape
# # A tibble: 3 x 3
#   x          Negative Positive
#   <chr>         <int>    <int>
# 1 Battery           3        3
# 2 Camera            3        3
# 3 Protection        3        3

最新更新