Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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
String R:用于搜索文本字符串并返回两个向量中的日期和价格的函数_String_R_List_Text_Dataframe - Fatal编程技术网

String R:用于搜索文本字符串并返回两个向量中的日期和价格的函数

String R:用于搜索文本字符串并返回两个向量中的日期和价格的函数,string,r,list,text,dataframe,String,R,List,Text,Dataframe,我得到以下提示: 给定一个文本字符串向量V.text,编写一个函数,从每个字符串中提取可能的美元金额和日期,并将其作为与V.text长度相同的列表的单独向量组件返回。金额和日期应以与输入格式完全相同的文本字符串形式返回。例如,如果其中一个输入字符串“在2009年5月1日以180000美元的价格列出,并在2009年1月3日以150250美元的价格出售”,则该元素的输出应该是一个包含两个向量的列表,一个表示金额,一个表示日期。金额应为“180000”和“150250美元”,日期应为“2009年5月1

我得到以下提示:

给定一个文本字符串向量V.text,编写一个函数,从每个字符串中提取可能的美元金额和日期,并将其作为与V.text长度相同的列表的单独向量组件返回。金额和日期应以与输入格式完全相同的文本字符串形式返回。例如,如果其中一个输入字符串“在2009年5月1日以180000美元的价格列出,并在2009年1月3日以150250美元的价格出售”,则该元素的输出应该是一个包含两个向量的列表,一个表示金额,一个表示日期。金额应为“180000”和“150250美元”,日期应为“2009年5月1日”和“2009年1月3日”

我尝试的解决方案是:

four <- function(x) {

  #split the data into individual observations
  lines <- str_split(x, "\n")

  n <- length(lines)
  list.date = NA; list.price = NA; sell.price = NA; sell.date = NA; temp = NA
  for (i in seq_len(n)) {
    list.date[i] <- word(x[i], 3)
    list.price[i] <- word(x[i], 5)
    sell.price[i] <- word(x[i], 9)
    sell.date[i] <- word(x[i], 11)
  }
  temp <- data.frame(list.date, list.price, sell.price, sell.date)
  temp
}

four如果没有看到许多可能的字符串,我想很难给出答案。这里有一些提示

了解正则表达式。这些是模式匹配模板,可以应用于字符串并获得匹配结果。例如,简单的数字匹配类似“\s[0-9]+\s”的内容,它转换为空格字符、一个或多个数字,然后是另一个空格。如果您知道数字至少为3位,请在“\s[0-9][0-9][0-9]+\s”上匹配。通过一些摆弄,你可以用美元符号和嵌入的逗号来匹配现金金额

您的日期与“[0-9]+/[0-9]+/[0-9]+”类似。当然,如果有人向您抛出带有“01/Jan/2010”的字符串,那么您需要一个regexp来匹配该字符串

所以,计算出可能出现的regexp,匹配它们,看看有多少匹配


help(regexp)
中的R将帮助您开始。

如果没有看到许多可能的字符串,我认为很难给出答案。这里有一些提示

了解正则表达式。这些是模式匹配模板,可以应用于字符串并获得匹配结果。例如,简单的数字匹配类似“\s[0-9]+\s”的内容,它转换为空格字符、一个或多个数字,然后是另一个空格。如果您知道数字至少为3位,请在“\s[0-9][0-9][0-9]+\s”上匹配。通过一些摆弄,你可以用美元符号和嵌入的逗号来匹配现金金额

您的日期与“[0-9]+/[0-9]+/[0-9]+”类似。当然,如果有人向您抛出带有“01/Jan/2010”的字符串,那么您需要一个regexp来匹配该字符串

所以,计算出可能出现的regexp,匹配它们,看看有多少匹配


R中的
help(regexp)
将帮助您入门。

使用
gregexpr
regmatches
举例说明前面的答案,例如:

ll <- c("Listed on 1/05/2009 for 180000 and sold for $150,250 on 3/1/2009",
        "Listed on 1/05/2012 for $300,400  and sold 120 for on 145,25")
## extract dates
dates <- regmatches(ll,gregexpr("[0-9]+\\/[0-9]+\\/[0-9]+",ll))
## remove dates 
ll <- gsub("[0-9]+\\/[0-9]+\\/[0-9]+",'',ll)
## extract amounts like 120 or 120,1254 
amounts <- regmatches(ll,gregexpr("\\$?[0-9]+(,[0-9]+)?",ll))

 dates
[[1]]
[1] "1/05/2009" "3/1/2009" 

[[2]]
[1] "1/05/2012"

> amounts
[[1]]
[1] "180000"   "$150,250"

[[2]]
[1] "$300,400" "120"      "145,25"  

使用
gregexpr
regmatches
给出先前答案想法的示例,例如:

ll <- c("Listed on 1/05/2009 for 180000 and sold for $150,250 on 3/1/2009",
        "Listed on 1/05/2012 for $300,400  and sold 120 for on 145,25")
## extract dates
dates <- regmatches(ll,gregexpr("[0-9]+\\/[0-9]+\\/[0-9]+",ll))
## remove dates 
ll <- gsub("[0-9]+\\/[0-9]+\\/[0-9]+",'',ll)
## extract amounts like 120 or 120,1254 
amounts <- regmatches(ll,gregexpr("\\$?[0-9]+(,[0-9]+)?",ll))

 dates
[[1]]
[1] "1/05/2009" "3/1/2009" 

[[2]]
[1] "1/05/2012"

> amounts
[[1]]
[1] "180000"   "$150,250"

[[2]]
[1] "$300,400" "120"      "145,25"  

你在哪里找到函数
stru-split
?@Arun我认为在
stringr
包中。@Arun是的Agstudy是正确的,它在stringr包中。你在哪里找到函数
stru-split
?@Arun我认为在
stringr
包中。@Arun是的Agstudy是正确的,它在stringr包中。谢谢,我会调查的。谢谢我会调查的。非常感谢,这太棒了!!非常感谢,这太棒了!!