按日期的唯一值

时间:2017-03-17 17:05:00

标签: r match unique

我想获得一个包含两列的数据框:1。不同的水果(没有重复)2。特定水果出现的第一个日期(即猕猴桃)

fruits <- c("apples, oranges, pears, bananas",
"pineapples, mangos, guavas",
"bananas, apples, kiwis") 


fruits<-as.data.frame(fruits)
fruits$date<-c( "12.8.16", "22.4.17", "12.9.16")

fruits[with(fruits, order(date)), ]

我尝试编写循环或使用match命令。但是,无法识别唯一的字符串值。

提前谢谢! Jannis

3 个答案:

答案 0 :(得分:3)

以下是一些解决方案:

1)strsplit / unnest / summarize 这使用dplyr和tidyr。首先将date列转换为"Date"类并拆分fruits列,生成一列,其中每个单元格包含一个水果矢量。 unnest那个并找到最小值:

library(dplyr)
library(tidyr)

fruits %>%
       mutate(date = as.Date(date, "%d.%m.%y"),
              fruits = strsplit(as.character(fruits), ", ")) %>%
       unnest %>%
       group_by(fruits) %>%
       summarize(date = min(date)) %>%
       ungroup

,并提供:

# A tibble: 8 × 2
      fruits       date
       <chr>     <date>
1     apples 2016-08-12
2    bananas 2016-08-12
3     guavas 2017-04-22
4      kiwis 2016-09-12
5     mangos 2017-04-22
6    oranges 2016-08-12
7      pears 2016-08-12
8 pineapples 2017-04-22

1a)separate_rows / summarize 这个略短的变体使用separate_rows(用一个更简单的命令替换strsplitunnest行。它需要tidyr 0.5或更高。它给出了相同的结果:

fruits %>%
       mutate(date = as.Date(date, "%d.%m.%y")) %>%
       separate_rows(fruits) %>%
       group_by(fruits) %>%
       summarize(date = min(date)) %>%
       ungroup

2)strsplit / stack / aggregate 这不使用任何包。首先,我们拆分fruits列,并使用日期命名结果列表L的组件。然后我们堆叠列表创建数据框并重命名列,同时还创建一个真正的"Date"类列。最后我们aggregate找到最小值。

L <- with(fruits, setNames(strsplit(as.character(fruits), ", "), as.Date(date,"%d.%m.%y")))
stk <- with(stack(L), data.frame(fruits = values, date = as.Date(ind)))
aggregate(date ~ fruits, stk, min)

给出这个data.frame:

      fruits       date
1     apples 2016-08-12
2    bananas 2016-08-12
3     guavas 2017-04-22
4      kiwis 2016-09-12
5     mangos 2017-04-22
6    oranges 2016-08-12
7      pears 2016-08-12
8 pineapples 2017-04-22

答案 1 :(得分:1)

这是一种使用 splitstackshape 包的方法,该包使用下面的 data.table 包。我们可以使用cSplit()fruits列拆分为逗号,然后使用 data.table 语法取最小值date

library(splitstackshape)
## create the long data frame from the split 'fruits' column
DT <- cSplit(fruits, "fruits", sep = ",", direction = "long")
## convert the 'date' column to date class and take the minimum row
DT[, .(date = min(as.IDate(date, "%d.%m.%y"))), by = fruits]
#        fruits       date
# 1:     apples 2016-08-12
# 2:    oranges 2016-08-12
# 3:      pears 2016-08-12
# 4:    bananas 2016-08-12
# 5: pineapples 2017-04-22
# 6:     mangos 2017-04-22
# 7:     guavas 2017-04-22
# 8:      kiwis 2016-09-12

答案 2 :(得分:0)

我认为这就是你所要求的。

fruits <- c("apples, oranges, pears, bananas",
        "pineapples, mangos, guavas",
        "bananas, apples, kiwis") 

fruits<-as.data.frame(fruits,stringsAsFactors=FALSE) #probably easier for the fruits to be strings rather than factors
fruits$date<-as.Date(c( "12.8.16", "22.4.17", "12.9.16"),format="%d.%m.%y") #and set your dates to be Dates rather than strings (otherwise they will be sorted alphabetically)

fruits[with(fruits, order(date)), ]

#need to convert your df to one-fruit-per-row
fruits2 <- do.call(rbind, #this binds together the data frames created by the lapply loop
               lapply(1:nrow(fruits), #loops through the rows of fruits df to create a list of data frames, each corresponding to one row
                      function(i) data.frame(fruit=trimws(strsplit((fruits$fruits),",")[[i]]), #splits your strings at commas, and trims off the whitespace
                                             date=fruits$date[i],stringsAsFactors = FALSE))) #adds the date corresponding to each row

#finding the first appearance is easily done using dplyr
library(dplyr)
fruits3 <- fruits2 %>% group_by(fruit) %>% summarise(firstdate=min(date))

或者另一种方法是使用水果的唯一名称设置数据框,然后使用grep查找每个水果找到第一个日期......

fruits <- fruits[order(fruits$date),]
firstfruits <- data.frame(fruit=unique(trimws(unlist(strsplit(fruits$fruits,",")))),stringsAsFactors = FALSE)
firstfruits$date <- do.call(c,lapply(firstfruits$fruit, function(F) fruits$date[grep(F,fruits$fruits)[1]]))
相关问题