0

My question is little tricky. I have a vector as below

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

And I have data frames as below Camera_pos # a Data Frame that has some columns (we may ignore the details here). Like wise we have other data frames such as Camera_neg, Battery_pos, Battery_neg, Protection_pos, Protection_neg

So I have 6 Data frame that holds some observations and those details are not of interest to the question.

Im trying to build a new Dataframe that pulls data/values from the vector and Data frames.

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
}

The code should be somewhat like this nrow(vec[i]_pos) i.e nrow(Camera_pos) in case of i =1. Request you to kindly help on this

P.S : Similarly the function should be able to call the elements in other vector too so the df has 3 rows and 3 columns filled

The output should be like below

Features        Positive         Negative
Camera          3                3
Battery         3                3
Protection      3                3
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
Arun Elangovan
  • 237
  • 1
  • 3
  • 16
  • Hello @Arun could you supply a reproducible example I refer you to [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451#5965451) question for details? it would be easier if we had dataframes to play with – Derek Corcoran Sep 30 '18 at 10:56
  • Hi Derek, thanks for the response. I have edited the question with few more details. Kindly reply in case you need more details. Thanks – Arun Elangovan Sep 30 '18 at 11:11

2 Answers2

1

This would be a way to do it:

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

I could explain in more detail if there is something you don't understand

Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54
1

Here's a tidyverse approach

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
AntoniosK
  • 15,991
  • 2
  • 19
  • 32