Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用R按间隔转换成数据库和标签_R_Database_Dataframe - Fatal编程技术网

如何用R按间隔转换成数据库和标签

如何用R按间隔转换成数据库和标签,r,database,dataframe,R,Database,Dataframe,我试图将大量数据转换成数据库,但它似乎太麻烦,无法使其有序化 这是示例数据 structure(list(V1 = structure(c(2L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 1L, 5L), .Label = c("", "01.01 ? 01.06 ", "02.01 ? 02.10 ", "03.01 ? 03.07

我试图将大量数据转换成数据库,但它似乎太麻烦,无法使其有序化

这是示例数据

structure(list(V1 = structure(c(2L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 
1L, 5L), .Label = c("", "01.01 ? 01.06                          ", 
"02.01 ? 02.10                          ", "03.01 ? 03.07                          ", 
"Chapter 4 (04.01~04.10)"), class = "factor"), V2 = structure(c(2L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 3L), .Label = c("", "CTH", "WO"
), class = "factor")), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-10L))
我想展示的结果是这样的

structure(list(V1 = c(1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 2.01, 
2.02, 2.03, 2.04, 2.05, 2.06, 2.07, 2.08, 2.09, 2.1, 3.01, 3.02, 
3.03, 3.04, 3.05, 3.06, 3.07, 4.01, 4.02, 4.03, 4.04, 4.05, 4.06, 
4.07, 4.08, 4.09, 4.1), V2 = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CTH", 
"WO"), class = "factor")), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-33L))
您可以发现A行的每个单元格都包含一个区间值。这将是我想制作的数据库中几个单元格的缩写

我应该将这些单元格标记为C行的相同代码-例如,01.01-CTH单元格、01.02-CTH单元格、01.03-CTH单元格,依此类推,直到完成0.06

但有几个问题

  • 如何将区间值单元格划分为两个变量
  • 以及根据间隔的第一个数据的值来标记相同的代码
  • 数据的分类指标为章节、标题和副标题
  • 我想,如果区间值的单元格只是第一章,我想把所有的子类别数据标记为与区间值对应的代码。 以及在新csv上组织数据帧
让我知道如何用R处理这些问题


谢谢大家

它不是特别优雅,但它完成了任务

library(dplyr)
library(stringr)
library(purrr)
library(tidyr)
input %>% 
  filter(V1 != "") %>% 
  mutate(lim = str_extract_all(V1, "\\d{2}\\.\\d{2}"),
         lim = map_chr(lim, paste0, collapse = ",")) %>% 
  separate(lim, 
           into = c("low", "high"),
           sep = ",") %>% 
  separate(low,
           into = c("prefix", "low"),
           sep = "\\.") %>% 
  mutate(high = str_replace(hight, "^.+\\.", ""),
         id = row_number()) %>% 
  select(id, V2, prefix, low, high) %>% 
  split(f = list(.$id)) %>% 
  map(.f = function(rdf){
    suffix <- (as.numeric(rdf$low):as.numeric(rdf$high)) / 100
    data.frame(V1 = as.numeric(rdf$prefix) + suffix,
               V2 = rep(rdf$V2, length(suffix)),
               stringsAsFactors = FALSE)
  }) %>% 
  bind_rows()
库(dplyr)
图书馆(stringr)
图书馆(purrr)
图书馆(tidyr)
输入%>%
过滤器(V1!=“”)%>%
突变(lim=str_extract_all(V1,“\\d{2}\\.\\d{2}”),
lim=map_chr(lim,paste0,collapse=“,”)%%>
独立(lim,
进入=c(“低”、“高”),
sep=“,”)%>%
单独(低,
into=c(“前缀”、“低”),
sep=“\\”)%>%
突变(high=str_替换(high,“^.+\\”,“),
id=行号())%>%
选择(id、V2、前缀、低、高)%>%
拆分(f=列表(.$id))%>%
映射(.f=函数(rdf){
后缀%
绑定_行()

这里是一种混合的基本R-stringr-data.table方法

library(stringr)
library(data.table)
aa = aa = structure(list(V1 = structure(c(2L, 1L, 1L, 3L, 1L, 4L, 1L, 1L, 1L, 5L), 
                          .Label = c("", "01.01 ? 01.06                          ", "02.01 ? 02.10                          ", "03.01 ? 03.07                          ", 
                                                "Chapter 4 (04.01~04.10)"), class = "factor"), 
           V2 = structure(c(2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 3L),
                          .Label = c("", "CTH", "WO"), class = "factor")), .Names = c("V1", "V2"),
      class = "data.frame", row.names = c(NA, -10L))
aa = aa[aa$V1 !="",]
a1 = str_trim(aa$V1)

cc  = str_split_fixed(aa$V1, "\\?|~", 2)
cc2 = apply(cc, 2, str_extract_all, "\\d+\\.\\d+", simplify = T)
cc2 = apply(cc2, 2, as.numeric)
cc2 = cbind(cc2, as.character(aa$V2))


range_col = list()
for(i in 1:nrow(cc2)){
  range_col[[i]] = data.table(V1 = seq(cc2[i,1], cc2[i,2], by = 0.01),
                              V2 = cc2[i, 3])
}
rr = rbindlist(range_col)
导致

      V1  V2
 1: 1.01 CTH
 2: 1.02 CTH
 3: 1.03 CTH
 4: 1.04 CTH
 5: 1.05 CTH
 6: 1.06 CTH
 7: 2.01 CTH
 8: 2.02 CTH
 9: 2.03 CTH
10: 2.04 CTH
11: 2.05 CTH
12: 2.06 CTH
13: 2.07 CTH
14: 2.08 CTH
15: 2.09 CTH
16: 2.10 CTH
17: 3.01 CTH
18: 3.02 CTH
19: 3.03 CTH
20: 3.04 CTH
21: 3.05 CTH
22: 3.06 CTH
23: 3.07 CTH
24: 4.01  WO
25: 4.02  WO
26: 4.03  WO
27: 4.04  WO
28: 4.05  WO
29: 4.06  WO
30: 4.07  WO
31: 4.08  WO
32: 4.09  WO
33: 4.10  WO

这也解释了当您有一个单独的数字…命名为您的数据
df

library(tidyverse)
as.character(df$V1) %>% 
  # extract all matching, ie 12.34
  str_extract_all(pattern = "\\d{2}\\.\\d{2}") %>% 
  # convert to matrix
  map(as.matrix) %>% 
  # transpose, such that it is only one column
  map(t) %>% 
  # convert to tibble (or data frame)
  map(as_tibble) %>% 
  # bind rows together, regardless of single or double rows
  reduce(bind_rows) %>% 
  # name columns
  set_names(nm = c("from", "to")) %>% 
  # if single values occur...
  mutate(to = ifelse(is.na(to), from, to),
         # add info of second column
         second = as.character(df$V2)) %>% 
  # drop missing rows
  drop_na() %>% 
  # convert to double for seq
  mutate_at(.vars = vars(from, to), as.double) %>% 
  group_by_all() %>% 
  # create list of individual length using seq
  mutate(first = list(seq(from = from, to = to, by = .01))) %>% 
  unnest() %>% ungroup() %>% 
  select(first, second)

哇,你真聪明……这帮了我很大的忙。@Benjamin我想你是用了“prefix”方法才艰难地完成的:-)您可以使用
创建一个序列,然后将它们拆分为行。我在下面发布了一个答案…请您评论,然后如何处理间隔是否不规则,以及间隔是什么样的(子数据)在另一个数据集上。它将类似于1.01=>1.012、1.32、1.325。如何扫描子数据并在新的数据帧上打印。?我认为将有一种方法来计算小数点并决定应该使用的间隔。如果您将此作为另一个问题发布,效果会更好。
library(tidyverse)
as.character(df$V1) %>% 
  # extract all matching, ie 12.34
  str_extract_all(pattern = "\\d{2}\\.\\d{2}") %>% 
  # convert to matrix
  map(as.matrix) %>% 
  # transpose, such that it is only one column
  map(t) %>% 
  # convert to tibble (or data frame)
  map(as_tibble) %>% 
  # bind rows together, regardless of single or double rows
  reduce(bind_rows) %>% 
  # name columns
  set_names(nm = c("from", "to")) %>% 
  # if single values occur...
  mutate(to = ifelse(is.na(to), from, to),
         # add info of second column
         second = as.character(df$V2)) %>% 
  # drop missing rows
  drop_na() %>% 
  # convert to double for seq
  mutate_at(.vars = vars(from, to), as.double) %>% 
  group_by_all() %>% 
  # create list of individual length using seq
  mutate(first = list(seq(from = from, to = to, by = .01))) %>% 
  unnest() %>% ungroup() %>% 
  select(first, second)