Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

r - Combine columns from one data frame into new data frame, and filter

Material    DocDate    Name  Address    Unit    Price
1258486   3/17/2017   FEHLIG BROS BOX    asd     8.95
1258486   5/11/2017   FEHLIG BROS BOX    asd     9.5
1258486   12/11/2017  FEHLIG BROS_BOX    asd     10.5
1250000   12/20/2017  Krones ALPHA       afg     11.5

I have a above data frame. I need to frame like below based on dates(3/17/2017) appears first. So i need below output

Material         Name/address/Unit Price
1258486     FEHLIG BROS BOX/asd/8.95/9.5/10.5
1250000     Krones/ALPHA/afg/11.5
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an alternative using dplyr. First the sample data:

data <- data.frame(stringsAsFactors=FALSE,
                   Material   = c(1258486L, 1258486L),
                   DocDate    = c("3/17/2017", "5/11/2017"),
                   Name       = c("FEHLIG BROS BOX", "FEHLIG BROS BOX"),
                   Address    = c("asd", "asd"),
                   Unit_Price = c(8.95, 9.5))

And then here are one set of steps to get your answer. (BTW, I believe all the solutions provided so far would give you multiple rows of output if there are multiple Material rows that share the same "earliest date." You might want another term like Unit_Price == min(Unit_Price) inside the filter if there's a tie-breaker that makes sense here.)

library(dplyr)
output <- data %>%

  # convert DocDate to a date
  mutate(DocDate = as.Date(DocDate,'%m/%d/%Y')) %>%

  # For each Material...
  group_by(Material) %>% 

  # just keep the line(s) with the first date...
  filter(DocDate == min(DocDate)) %>% ungroup() %>% 

  # and combine fields
  mutate(`Name/address/Unit Price` = paste(Name, Address, Unit_Price, sep = "/")) %>%

  # just the requested columns
  select(Material, `Name/address/Unit Price`)

output
# A tibble: 1 x 2
  Material `Name/address/Unit Price`
     <int> <chr>                    
1  1258486 FEHLIG BROS BOX/asd/8.95 

(EDIT: fixed typos in code)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...