我在SQL中看到这样的数据。
<table class="detailList" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="data2Col first " colspan="2">
<div>
<span class="labelCol vfLabelColTextWrap" scope="row" style="display:inline-block; ">Name </span>
<span class="data2Col"> <a href="/1233" onclick="openTab('1233','Erich Schulz'); return false" target="_blank">Erich Schulz</a> </span>
</div>
</td>
</tr>
<tr>
<th class="labelCol vfLabelColTextWrap " scope="row">Title</th>
<td class="data2Col "><span id="j_id0:j_id1:j_id3:j_id4:j_id6"></span></td>
</tr>
<tr>
<th class="labelCol vfLabelColTextWrap " scope="row">Phone</th>
<td class="data2Col "><span id="j_id0:j_id1:j_id3:j_id4:j_id7">(555) 555-5555</span></td>
</tr>
<tr>
<th class="labelCol vfLabelColTextWrap last " scope="row">Email</th>
<td class="data2Col last "><span id="j_id0:j_id1:j_id3:j_id4:j_id8"><a href="mailto:test@example.com">test@example.com</a></span></td>
</tr>
</tbody>
</table>
尝试在R中使用以下代码来获取我想要的表:
date_at price
1 2016-03-02 631USD/1M
2 2016-03-02 741USD/1M
3 2016-03-02 702USD/1M
4 2016-03-02 685USD/1M
5 2016-03-02 738USD/1M
6 2016-03-02 741USD/1M
即,我试图将SQL tbl变为跟随,然后选择变量:
df <- tbl(db,"table") %>%
mutate(newprice = as.numeric(substr(price,1,regexpr("USD",price)-1))) %>%
select(date, newprice) %>%
head()
但是,我无法使用上述代码创建newprice列。得到此错误:
date_at price newprice
1 2016-03-02 631USD/1M 631
2 2016-03-02 741USD/1M 741
3 2016-03-02 702USD/1M 702
4 2016-03-02 685USD/1M 685
5 2016-03-02 738USD/1M 738
6 2016-03-02 741USD/1M 741
我发现mutate只能处理应用于变量的一个函数。我应该做什么其他功能
答案 0 :(得分:0)
这可以帮到你:
df<-data.frame(date_at=c("2016-03-02","2016-03-02","2016-03-02","2016-03-02",
"2016-03-02","2016-03-02"),
price=c("631USD/1M","741USD/1M","702USD/1M","685USD/1M",
"738USD/1M","741USD/1M"))
df%>%
tbl_df%>%
mutate(newprice=as.numeric(substr(price,1,regexpr("USD",price)-1)),
date_at=as.Date(date_at))%>%
select(date_at,newprice)%>%
head()
Source: local data frame [6 x 2]
date_at newprice
(date) (dbl)
1 2016-03-02 631
2 2016-03-02 741
3 2016-03-02 702
4 2016-03-02 685
5 2016-03-02 738
6 2016-03-02 741