映射到字符串,然后对结果进行pivot_wider

时间:2020-08-04 13:51:30

标签: r

我有一些要计算的数据和pivot_wider。数据如下:

1   second
2   c("fourth", "fourth", "fourth", "fourth")
3   c("second", "second")
4   c("second", "second")
5   second
6   c("second", "third", "second")
7   fourth
8   fourth
9   c("third", "fourth")
10  c("fourth", "fourth", "fourth", "fourth", "fourth", "fourth")

我正在尝试使数据看起来像这样:

    first second third fourth
1            1
2                         4
3            2
4            2
5            1
6            2     1
7                         1
8                         1
9                  1      1
10                        6

数字仅是单词在列中出现的次数的计数

数据

d1 <- structure(list(quarterMentioned = list("second", c("fourth", 
"fourth", "fourth", "fourth"), c("second", "second"), c("second", 
"second"), "second", c("second", "third", "second"), "fourth", 
    "fourth", c("third", "fourth"), c("fourth", "fourth", "fourth", 
    "fourth", "fourth", "fourth"))), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

3 个答案:

答案 0 :(得分:2)

一种 let currentDate = Date() guard let subscriptionPeriod = product.subscriptionPeriod else { return 0 } let numberOfUnits = subscriptionPeriod.numberOfUnits let unit = subscriptionPeriod.unit var calendarComponent: Calendar.Component? = nil switch unit { case .day: calendarComponent = .day break case .month: calendarComponent = .month break case .week: calendarComponent = .weekOfYear break case .year: calendarComponent = .year break default: break } guard let component = calendarComponent else { return 0 } let calendar = Calendar(identifier: .iso8601) guard let newDate = calendar.date(byAdding: component, value: numberOfUnits, to: currentDate) else { return 0 } return newDate.timeIntervalSince1970 - currentDate.timeIntervalSince1970 } 解决方案可能是:

tidyr

答案 1 :(得分:1)

您可以创建一个行号列,以长格式unnest来获取数据,并使用pivot_wider以计数的形式获取宽格式。

library(dplyr)
library(tidyr)

d1 %>%
  mutate(row = row_number()) %>%
  unnest(quarterMentioned) %>%
  pivot_wider(names_from = quarterMentioned, values_from = quarterMentioned, 
              values_fill = 0, values_fn = length)

# A tibble: 10 x 4
#     row second fourth third
#   <int>  <int>  <int> <int>
# 1     1      1      0     0
# 2     2      0      4     0
# 3     3      2      0     0
# 4     4      2      0     0
# 5     5      1      0     0
# 6     6      2      0     1
# 7     7      0      1     0
# 8     8      0      1     0
# 9     9      0      1     1
#10    10      0      6     0

答案 2 :(得分:1)

这是基本的R选项

v <- c("first","second","third","fourth")
as.data.frame(do.call(rbind,Map(function(x) table(factor(x,levels = v)),d1$quarterMentioned)))

这样

   first second third fourth
1      0      1     0      0
2      0      0     0      4
3      0      2     0      0
4      0      2     0      0
5      0      1     0      0
6      0      2     1      0
7      0      0     0      1
8      0      0     0      1
9      0      0     1      1
10     0      0     0      6