我有以下数据集,已经按交易排序:
dataset <- data.frame(id = c(1,2,3,4,2,4,6,7,3,2),
transaction = c(1,2,3,4,5,6,7,8,9,10),
amount = c(200,100,50,100,50,300,100,50,100,50))
Run Code Online (Sandbox Code Playgroud)
如您所见,每个客户都有一个 ID 和在交易中花费的金额。
我的问题是,如何确定客户是交易中的新客户还是经常性客户。新客户意味着这是它的第一笔交易,接下来的交易是经常性的。
recurrence_status <- c("new","new","new","new","recurrent",
"recurrent","new","new","recurrent","recurrent")
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了以下方法:
for (i in 1:(length(dataset$transaction)-1)){
for(j in 2:length(dataset$transaction)){
j <- j + 1
comp <- dataset[j:length(dataset$id)]
ifelse((is.element(dataset[i,1]),comp),"recurrent","new")
}
}
Run Code Online (Sandbox Code Playgroud)
但是由于括号,它给了我一个错误。我知道应该尽可能避免在 R 中使用循环。请,任何帮助将受到欢迎。
问候,
I have the following dataset
CREATE TABLE my_table
(
the_debt_id varchar(6) NOT NULL,
the_debt_paid date NOT NULL,
the_debt_due date NOT NULL
)
INSERT INTO my_table
VALUES ('LMUS01', '2019-05-03', '2019-05-02'),
('LMUS01', '2019-06-03', '2019-06-02'),
('LMUS01', '2019-07-01', '2019-07-02'),
('LMUS02', '2019-05-03', '2019-05-07'),
('LMUS02', '2019-06-07', '2019-06-07')
Run Code Online (Sandbox Code Playgroud)
我想通过仅过滤the_debt_paid2019 年 6 月的行来查询该数据集。
预期结果是:
the_debt_id the_debt_paid the_debt_due
LMUS01 2019-06-03 2019-06-02
LMUS02 2019-06-07 2019-06-07
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
SELECT * FROM my_table
WHERE EXTRACT(month, the_debt_paid) = 6
Run Code Online (Sandbox Code Playgroud)
但我不知道如何申请 2019 年。任何帮助将不胜感激。
我有以下数据集:
dataset <- data.frame(id = c("A","A","A","A","B","B","B,"B"),
value = c(1,1,2,3,5,6,6,7))
Run Code Online (Sandbox Code Playgroud)
对于每个重复的 id,我想标记它发生的行,并且这个标志应该与数据帧源的长度相同。这是预期的结果:
id value flag
A 1 1
A 1 1
A 2 0
A 3 0
B 5 0
B 6 1
B 6 1
B 7 0
Run Code Online (Sandbox Code Playgroud)
有没有办法让我不必使用 for 循环?任何帮助将不胜感激。
我有以下示例 Flexdashboard:
---
title: "Hover"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
data(iris)
Run Code Online (Sandbox Code Playgroud)
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
iris %>% group_by(Species) %>%
summarise(mean = mean(Sepal.Length)) %>%
ggplot(aes(Species, mean)) + geom_col()
Run Code Online (Sandbox Code Playgroud)
我想要的是当鼠标悬停在图中的条形图上时的工具提示并显示其值。我在《SO How do I show the y value on tooltip while hover in ggplot2》中读到了这篇文章,但它适用于 Shiny。我试过这个:
p <- iris %>% group_by(Species) %>%
summarise(mean = mean(Sepal.Length))
labels <- sprintf("<strong>%s</strong><br/>Mean: %f",
p$Species, p$mean) %>%
lapply(htmltools::HTML)
p %>% ggplot(aes(Species, mean)) + geom_col() + …Run Code Online (Sandbox Code Playgroud) 我有以下数据集:
the_data <- data.frame(the_col = "a.1","b.2","c.3","d.4")
Run Code Online (Sandbox Code Playgroud)
我尝试将其分成两列。这似乎是一个重复的问题,但让它不同的是我想要的分隔符(点)。我试过:
the_data %>% separate(the_col, into = c("alfa","beta"), sep = ".")
Run Code Online (Sandbox Code Playgroud)
但我收到警告而不是我想要的:
alfa beta X.b.2. X.c.3. X.d.4.
1 b.2 c.3 d.4
Run Code Online (Sandbox Code Playgroud)
我想要的是:
alfa beta
a 1
b 2
c 3
d 4
Run Code Online (Sandbox Code Playgroud)
请你帮助我好吗?谢谢。
我从谷歌工作表中检索了一张工作表,并返回了一个列表列表:
sheet <- list(var1 = list(Sys.time(), Sys.time(),NULL),
var2 = list(NULL,1,2),
var3 = list("a",NULL,"b"))
Run Code Online (Sandbox Code Playgroud)
我必须将列表中的所有成员转换NULL为NAs. 我被善意地解释了如何使用例如列表来完成它sheet$var1[sapply(sheet$var1,is.null)] <- NA,但是由于我有很多变量,是否可以仅在一行中对其进行编码?任何帮助将不胜感激。
我有个约会,比方说 2020 年 7 月 7 日。
my_date <- as.Date("2020/07/07")
Run Code Online (Sandbox Code Playgroud)
我想将这个日期抵消一个月。所以它应该在 2020 年 8 月 7 日返回。
我试过,my_date + 30但如果一个月有 30 或 31 天,它就不起作用。
编辑
我的问题必须准确。如果日期是 2020 年 5 月 31 日怎么办?它应该抵消到 2020 年 6 月 30 日。请在 R 基础上提供帮助。
任何帮助将不胜感激。
我有以下创建 Flexdashboard 的代码:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
Run Code Online (Sandbox Code Playgroud)
我想插入一些 HTML 和 javascript 代码。我试过这个
Column
-----------------------------------------------------------------------
### Block 1
```{r}
<p>"This is a paragraph"</p>
<script>
alert("This is an alert")
</script>
```
Run Code Online (Sandbox Code Playgroud)
但这不起作用。请问您能帮我解答这个问题吗?谢谢。
我有一个带有 python 3.6.9 的 EC2 Ubuntu 18.04.3 LTS 实例
我尝试使用以下命令安装 pyarrow:
python3 -m pip install pyarrow
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;
__file__='/tmp/pip-build-3q5nmx81/pyarrow/setup.py';
f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');
f.close();exec(compile(code, __file__, 'exec'))"
install --record /tmp/pip-fgafmfzg-record/install-record.txt
--single-version-externally-managed --compile --user --prefix="
failed with error code 1 in /tmp/pip-build-3q5nmx81/pyarrow/
Run Code Online (Sandbox Code Playgroud)
尝试安装 yarrow 后阅读第一条消息,我看到:
Building wheels for collected packages: pyarrow
Running setup.py bdist_wheel for pyarrow ... error
Run Code Online (Sandbox Code Playgroud)
请问您知道我该如何解决这个问题吗?先感谢您。