mercredi 15 août 2018

Replacing data wrangling with conditionals

I'm used to working in R, but I recently started a new job, where working in R is not always a possibility. When it's not - like in this case - you need to do your data wrangling with simple conditionals, which works out most of the time. However, today I stumbled upon a problem, that I just can't seem to crack. The problem is that I have the following data frame:

> df
   ID Group Source Price
1   1 TG001 System    50
2   1    NA   File    50
3   2 TG002 System    78
4   2    NA   File    78
5   4 TG004 System    65
6   4    NA   File    65
7   3 TG003 System    74
8   3    NA   File    74
9   5 TG005 System    99
10  5    NA   File    99

I need to swap the NA's for the corresponding value with the same ID, and then arrange the dataset after the "Group" variable. I get the desired result with the following code:

df <- read.csv2("sysExtract.csv")

df2 <- df %>% 
  spread(Source, Group)

df2$File <- df2$System

df <- df2 %>% 
  gather(Source, Group, 3:4) %>% 
  select(ID, Group, Source, Price) %>% 
  arrange(Group)

However, as I mentioned I can't use R for this problem, and the system I need to use relies only on conditionals for these type of operations. Does anyone here have any ideas?

Aucun commentaire:

Enregistrer un commentaire