I have this data frame:
d
structure(list(Product = structure(c(3L, 1L, 2L, 4L, 4L, 6L,
4L, 5L), .Label = c("App_Servers ", "Db_servers,application ",
"Server1,Serve2,Server4", "Server1,Serve2,Server4 ", "Server1,Serve2,Server4 ",
"Server1,Serve2,Sever4 "), class = "factor"), Day = structure(c(3L,
5L, 4L, 5L, 2L, 4L, 1L, 1L), .Label = c("Mon ", "Thu ", "Tue",
"Tue ", "Wed "), class = "factor"), Date = structure(c(1L, 2L,
3L, 4L, 5L, 6L, 7L, 7L), .Label = c(" 2015-01-06 ", "2015-01-07 ",
"2015-01-13 ", "2015-01-14 ", "2015-01-15 ", "2015-01-20 ", "2015-02-16 "
), class = "factor"), Month = structure(c(2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L), .Label = c("Feb", "Jan"), class = "factor")), .Names = c("Product",
"Day", "Date", "Month"), class = "data.frame", row.names = c(NA,
-8L))
I need to be able to put dates in one cell separated by comma that are grouped by Product, Day and Month. For example,
Server1,Serve2,Server4 appears on 2015-01-06, 2015-01-14, 2015-01-15, 2015-01-20 for the month of Jan.
my new df needs to look like this:
Product Day Date Month Day_list
Server1,Serve2,Server4 Tues 2015-01-06 Jan 2015-01-06,2015-01-13,2015-01-20
Any packages that can help me do this in R?
I tried using data.table package:
d[,d:=paste(Date,Date), c("Product","Day","Month")]
not working
There are a couple of things here.
First, your columns have additional spaces in them. You'd have to remove so that you can group them together.
require(data.table)
setDT(d)[, `:=`(Product = gsub("[ ]", "", Product),
Date = gsub("[ ]", "", Date))]
Second, you're using paste()
and :=
wrongly.
d[, Date_list := paste(Date, collapse=","), by=c("Product", "Month")]
d
# Product Day Date Month Date_list
# 1: Server1,Serve2,Server4 Tue 2015-01-06 Jan 2015-01-06,2015-01-14,2015-01-15
# 2: App_Servers Wed 2015-01-07 Jan 2015-01-07
# 3: Db_servers,application Tue 2015-01-13 Jan 2015-01-13
# 4: Server1,Serve2,Server4 Wed 2015-01-14 Jan 2015-01-06,2015-01-14,2015-01-15
# 5: Server1,Serve2,Server4 Thu 2015-01-15 Jan 2015-01-06,2015-01-14,2015-01-15
# 6: Server1,Serve2,Sever4 Tue 2015-01-20 Jan 2015-01-20
# 7: Server1,Serve2,Server4 Mon 2015-02-16 Feb 2015-02-16,2015-02-16
# 8: Server1,Serve2,Server4 Mon 2015-02-16 Feb 2015-02-16,2015-02-16
Have a look at the Introduction to data.table and Reference semantics vignettes.
Edit: I just realised that the 6th row has a typo for Product
. It has Sever4
instead of Server4
.
Here is one solution using dplyr
:
d %>% mutate(
Product = gsub("[ ]", "", Product),
Day = gsub("[ ] ", "", Day )
) %>%
group_by(Product, Month) %>%
mutate(
Day_list = paste(Date, collapse = "")
)
Product Day Date Month Day_list
1 Server1,Serve2,Server4 Tue 2015-01-06 Jan 2015-01-06 2015-01-14 2015-01-15
2 App_Servers Wed 2015-01-07 Jan 2015-01-07
3 Db_servers,application Tue 2015-01-13 Jan 2015-01-13
4 Server1,Serve2,Server4 Wed 2015-01-14 Jan 2015-01-06 2015-01-14 2015-01-15
5 Server1,Serve2,Server4 Thu 2015-01-15 Jan 2015-01-06 2015-01-14 2015-01-15
6 Server1,Serve2,Sever4 Tue 2015-01-20 Jan 2015-01-20
7 Server1,Serve2,Server4 Mon 2015-02-16 Feb 2015-02-16 2015-02-16
8 Server1,Serve2,Server4 Mon 2015-02-16 Feb 2015-02-16 2015-02-16
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.