Quantcast
Channel: Convert multiple entries to a single row with a vector column in R - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Convert multiple entries to a single row with a vector column in R

$
0
0

I have a dataset with multiple rows that describes one user. I am trying to change my dataset to be one row represents one user.

Reproducible Example:

old_way <- data.frame("Day" = 1:10, "Purchase" = 20:29, "Name" = c("John", "John", "John", "Dora", "Dora", "Dora", "Dora", "Gerald", "Gerald", "Gerald"), stringsAsFactors = FALSE)   Day Purchase   Name1    1       20   John2    2       21   John3    3       22   John4    4       23   Dora5    5       24   Dora6    6       25   Dora7    7       26   Dora8    8       27 Gerald9    9       28 Gerald10  10       29 Gerald

The big difference being each row is now one person. So when I am doing records, I can very easily see which days they were here and what purchase they did.

desired_way <- data.frame("Name" = c("John","Dora","Gerald"), "Day" = c("1, 2, 3", "4, 5, 6, 7", "8, 9 ,10"), "Purchase" = c("20, 21, 22", "23, 24, 25, 26", "27, 28, 29"), "Last_Day" = c("3", "7", "10"), "Avg_Purchase" = c("21","25","28"))    Name           Day          Purchase    Last_Day Avg_Purchase1   John       1, 2, 3        20, 21, 22           3           212   Dora    4, 5, 6, 7    23, 24, 25, 26           7           253 Gerald      8, 9 ,10        27, 28, 29          10           28

How could I create that cell that encapsulates the other rows information? And does R support operations done on that cell, or would I need to calculate the most recent and average when I am creating that cell?

Thank you all in advance!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images