0

I've got a dataframe structured like this:

df = data.frame(topic = c("xxx", "xxx", "yyy", "yyy", "yyy", "zzz", "zzz"), 
             high = c(52L, 27L, 89L, 99L, 43L, 21L, 90L), 
             low = c(56L, 98L, 101L, 21L, 98L, 40L, 43L), 
             stringsAsFactors = FALSE)

I would like to create a single variable for each unique value in the topic column, while keeping all the observations untouched. Basically it's like looping this dplyr filter:

zzz = df %>% filter (topic == "zzz")

It should be easy, so I'm sure I'm missing some basic knowledge here... Thanks!


Edit: It was my first question on stackoverflow, I apologize for the bad formatting.

SkuPak
  • 307
  • 8
  • 16
  • Welcome to Stack Overflow! Please provide a [reproducible example in r](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). The link I provided, will tell you how. Moreover, please take the [tour](https://stackoverflow.com/tour) and visit [how to ask](https://stackoverflow.com/help/how-to-ask). There's `topic` column, not row! What do you mean by *"single variable"*? Please provide your desired output! In the meantime look into `group_by()` – M-- Feb 13 '19 at 20:42
  • `df %>% split(list(.$topic))` this will give you a list of dataframes. you can extract those dataframes and save them in the environment (look at this [link](https://stackoverflow.com/questions/38310741/how-to-save-the-elements-of-a-list-individually-in-r)) – M-- Feb 13 '19 at 20:51

2 Answers2

0
df <- data.frame(topic = c("xxx", "xxx", "yyy", "yyy", "yyy", "zzz", "zzz"), 
                  high = c(52L, 27L, 89L, 99L, 43L, 21L, 90L), 
                  low = c(56L, 98L, 101L, 21L, 98L, 40L, 43L), 
                 stringsAsFactors = FALSE)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

for (variable in unique(df$topic)) {
  assign(variable, df %>% filter (topic == variable), envir = .GlobalEnv)
}

xxx
#>   topic high low
#> 1   xxx   52  56
#> 2   xxx   27  98

yyy
#>   topic high low
#> 1   yyy   89 101
#> 2   yyy   99  21
#> 3   yyy   43  98

zzz
#>   topic high low
#> 1   zzz   21  40
#> 2   zzz   90  43

Created on 2019-02-13 by the reprex package (v0.2.1)

Kerry Jackson
  • 1,821
  • 12
  • 20
  • 1
    Please test your code before posting an answer. You need `... assign(as.character(variable) ...` otherwise this error would pop up: `Error in assign(variable, df %>% filter(topic== variable), envir = .GlobalEnv) : invalid first argument` – M-- Feb 13 '19 at 20:58
0

you can try this:

topics <- unique(df$topic)  
y <- lapply(1:length(topics), function(x) {dt %>% filter(topic==topics[x])})
names(y) <- topics
list2env(y , envir = .GlobalEnv)

Best!

LocoGris
  • 4,432
  • 3
  • 15
  • 30