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

How to call a Dataframe and find the nrow within a function using R?

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

vec <-c("Camera","Battery","Protection")

我有如下数据框 Camera_pos # 一个包含一些列的数据框(我们可以忽略这里的细节)。同样,我们还有其他数据框,例如 Camera_neg、Battery_pos、Battery_neg、Protection_pos、Protection_neg

所以我有 6 个数据框,其中包含一些观察结果,这些细节与问题无关。

我正在尝试构建一个新的数据框,从矢量和数据框中提取 data/values。

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

这是一种方法:

#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