I have a df:
product store store1 review review1
book A B
shirt A B
pen A B
cd A B 0 2
dress A B 2 1
magazine A B 3 1
I want the values in the store columns to become column names and I want to insert the review values in that column, so the output looks like this:
product A B
book 0 2
shirt 2 1
pen 3 1
There are two issues with this problem. First of all, the store names will change a lot in the future, so I can't use code like this:
names(newdf)[names(newdf) == 'store'] <- 'a'
Secondly, I need the values from the review and review1 column to start from the first row in the A and B column, so to say. For example, column a = book = 0, shirt = 2, magazine = 3.
I'm really stuck on this, any help would be much appreciated!
Reproducible code:
df <- data.frame(product = c("book","shirt", "pen", "cd", "dress", "magazine"), store=c("A", "A", "A", "A", "A", "A"),
store1=c("B", "B", "B", "B", "B", "B"), review=c("", "", "", 0, 2, 3), review1 =c("", "", "", 2, 1, 1))