【问题标题】:How to call a Dataframe and find the nrow within a function using R?如何使用 R 调用 Dataframe 并在函数中查找 nrow?
【发布时间】: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


【解决方案1】:

这是一种方法:

#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
}

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

【讨论】:

    【解决方案2】:

    这是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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 2023-01-19
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      相关资源
      最近更新 更多