0

I have a file with some codes and I want to add 2 columns which has fixed set of values in it. and I want to put: for each code in the file, put 2 columns with fixed set of values in it.

Using Reshape or tidyr or dplyr in R.

enter image description here For example: some_codes.csv has: " codes 123 234 r345 "

and i want to create 2 columns in this (set and category) which has fixed values.

codes set category 123 1 a 123 2 b 123 3 c 234 1 a 234 2 b 234 3 c 345 1 a 345 2 b 345 3 c

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 3
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Feb 20 '20 at 19:27

1 Answers1

0

Here is a solution.

For each code, you cbind the value and the dataframe. purrr::map_dfr will then join everything together as a dataframe by binding rows.

library(tidyverse)
codes = data.frame(codes=c(123,234,345))
categ = data.frame(set=1:3, categ=c("a","b","c"))
codes$codes %>% map_dfr(~{cbind(code=.x,categ)})

But as Tung said, you should post a reproducible example next time. You can use the dput function for this.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92