【问题标题】:how to count several strings assigned to a column?如何计算分配给一列的几个字符串?
【发布时间】:2017-11-21 23:03:10
【问题描述】:

我最近问了一个问题得到了很好的回答,我感谢帮助我的人how to count the numbers based on two columns

df2<- structure(list(V1 = structure(c(1L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 
NA, NA, NA, NA, 4L, 2L, 3L, 5L, NA, 1L, 1L, 1L, 1L, 5L, 6L, 6L, 
6L, NA, NA, 5L), .Label = c("1 x Bruit (U)", "1 x Bruit (U) 1 x TAMAN (M)", 
"1 x Bruit (U) 2 x TAMAN (M)", "1 x TAMAN (M) 2 x TAMAN (M)", 
"2 x Bruit (U)", "2 x TIKIam(T)"), class = "factor"), V2 = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("BUX1_T10963", 
"BUX1_T10964", "BUX1_T10965", "BUX1_T10966", "BUX2_T10076"), class = "factor")), .Names = c("V1", 
"V2"), class = "data.frame", row.names = c(NA, -28L))

我还有一个问题,我认为我应该问一个不同的问题而不是同一个问题。

如果我在第一列中有多个字符串,那么我无法获得好的结果。例如上面的问题。我明白了

如果我执行以下操作

library(stringr)
library(reshape2)
df$goodname = gsub(pattern = "^[0-9]+ x ", replacement = "", x = df$V1)
# extract the number
df$quantity = as.numeric(str_extract(df$V1, "^[0-9]+"))
# any missing values assume to be 1
df$quantity[is.na(df$quantity)] = 1
dcast(data = df, formula = goodname ~ V2, value.var = "quantity", fun.aggregate = sum, na.rm = T)

我得到了类似的答案

                 goodname BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
1               Bruit (U)           5           9           2           6           2
2 Bruit (U) 1 x TAMAN (M)           0           0           1           0           0
3 Bruit (U) 2 x TAMAN (M)           0           0           1           0           0
4 TAMAN (M) 2 x TAMAN (M)           0           0           1           0           0
5               TIKIam(T)           0           0           0           6           0
6                    <NA>           0           4           1           2           0

但我想得到这样的答案

                goodname BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
1               Bruit (U)           5           9           4           6       2
2               TAMAN (M)           0           0           6           0       0
3               TIKIam(T)           0           0           0           6       0
4                    <NA>           0           4           1           2       0
5               TTXI1(M)            0           0           0            2       0

【问题讨论】:

  • 如何在TAMAN (M) 中获得BUX1_T109656
  • @discipulus 1 x 塔曼 (M) + 2 x 塔曼 (M) + 塔曼 (M) 2 x 塔曼 (M)

标签: r string reshape


【解决方案1】:

使用dplyrtidyr 的解决方案。我们有不同的输出,但由于您的df2 没有任何TTXI1(M),我认为我的输出可能更有意义。关键是使用separate_rows来扩展多条记录的行,使用separate来分隔Times1 x2 x)和Label

library(dplyr)
library(tidyr)

df3 <- df2 %>%
  separate_rows(V1, sep = "(?<=\\))\\s(?=[0-9]+)") %>%
  separate(V1, into = c("Times", "Label"), sep = " x ", convert = TRUE) %>%
  mutate(Times = ifelse(is.na(Times), 1, Times)) %>%
  group_by(Label, V2) %>%
  summarise(n = sum(Times)) %>%
  spread(V2, n, fill = 0) %>%
  ungroup()
df3

# # A tibble: 4 x 6
#       Label BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
# *     <chr>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>
# 1 Bruit (U)           5           9           4           6           2
# 2 TAMAN (M)           0           0           6           0           0
# 3 TIKIam(T)           0           0           0           6           0
# 4      <NA>           0           4           1           2           0

【讨论】:

  • 我再次喜欢并接受了您的回答。你能解释一下你的代码吗?我想学它
  • 谢谢。您想了解更多功能?了解哪个函数执行什么操作的一种方法是一次运行多行代码并比较输出数据帧。
  • 我想知道您是如何得出将这些功能结合起来的结论的?我应该读什么才能获得这种能力?
  • @nik 我鼓励你找到dplyrtidyr 的教程。这两个包非常有用。
猜你喜欢
  • 1970-01-01
  • 2023-01-13
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 2013-08-04
  • 2019-10-13
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多