我最近开始使用该R包,openxlsx因为它根本不需要rJava包或Java.我正在尝试将日期写入列,但行格式未应用于我的xlsx文件中的最终日期列.
示例数据:
tmp <- structure(list(someNumbers = c(8L, 3L, 4L, 4L, 1L, 4L, 2L, 2L,
7L, 2L), someLetters = structure(c(9L, 4L, 1L, 2L, 7L, 3L, 4L,
6L, 8L, 5L), .Label = c("b", "d", "e", "h", "l", "n", "o", "t",
"u"), class = "factor"), moreNumbers = c(25L, 25L, 36L, 38L,
39L, 32L, 31L, 40L, 28L, 32L), moreStuff = c(0.37, 1.39, -2.27,
0.65, -0.71, -1.67, 0.71, 1.32, 2.14, 1.13), words = structure(c(5L,
3L, 7L, 1L, …Run Code Online (Sandbox Code Playgroud) 是否有一个"最佳实践"来记录F#中的歧视联盟?我一直在使用MSDN网站上XML提供的标签,但没有提及记录除标签之外的DU .<typeparam name = "x"> Desc. </typeparam>
标签有助于标准类型和功能,但是哪些XML标签应该用于DU?
假设我有一个起始列表
let test = [1; 2; 3; 4; 5]
Run Code Online (Sandbox Code Playgroud)
我想将第一个元素添加到所有其他元素,然后将第二个元素添加到所有其他元素,依此类推,直到我最终得到像
result = [1, [3; 4; 5; 6];
2, [3; 5; 6; 7];
3, [4; 5; 7; 8];
4, [5; 6; 7; 9];
5, [6; 7; 8; 9]]
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我无法弄清楚哪种List.n方法适合这里,甚至是否有一种方法.我知道List.map不会这样做,我也不认为List.reduce或List.fold将会这样做,因为这些都是累加器而不是单独的功能.
有没有办法做我想做的事情?
谢谢你的帮助
我正在尝试decimal options使用自定义类型进行一些数学运算:
type LineItem = {Cost: decimal option; Price: decimal option; Qty: decimal option}
let discount = 0.25M
let createItem (c, p, q) =
{Cost = c; Price = p; Qty = q}
let items =
[
(Some 1M , None , Some 1M)
(Some 3M , Some 2.0M , None)
(Some 5M , Some 3.0M , Some 5M)
(None , Some 1.0M , Some 2M)
(Some 11M , Some 2.0M , None)
]
|> List.map createItem
Run Code Online (Sandbox Code Playgroud)
我可以做一些非常简单的算术 …
我很难加入Map.remove一系列密钥.
例如,如果我有
let z : Map<int, Set<int>> = Map.ofList [(1, set []); (3, set [3; 4]); (4, set [])]
Run Code Online (Sandbox Code Playgroud)
我想programmaticaly找到每个key, value在其中value是空的,我可以这样做:
let emptySets =
z
|> Map.toList
|> List.map (fun (k, s) -> (k, s |> Set.toList |> List.length))
|> List.filter (fun (k, i) -> i = 0)
|> List.map fst
Run Code Online (Sandbox Code Playgroud)
这给了我
[1; 4]
Run Code Online (Sandbox Code Playgroud)
这正是我所期待的.但是现在,如果我想key, value从地图中删除这些对,我能想到的唯一方法就是将原始内容Map转换为如下列表:
z
|> Map.toList
|> List.filter (fun (k, s) -> not (emptySets
|> List.contains k)) …Run Code Online (Sandbox Code Playgroud) 我有一个示例数据帧
df <- data.frame(cust = sample(1:100, 1000, TRUE),
channel = sample(c("WEB", "POS"), 1000, TRUE))
Run Code Online (Sandbox Code Playgroud)
我正试图改变
get_channels <- function(data) {
d <- data
if(unique(d) %>% length() == 2){
d <- "Both"
} else {
if(unique(d) %>% length() < 2 && unique(d) == "WEB") {
d <- "Web"
} else {
d <- "POS"
}
}
return(d)
}
Run Code Online (Sandbox Code Playgroud)
这没有问题,在小型数据帧上,它根本不需要时间.
start.time <- Sys.time()
df %>%
group_by(cust) %>%
mutate(chan = get_channels(channel)) %>%
group_by(cust) %>%
slice(1) %>%
group_by(chan) %>%
summarize(count = n()) %>%
mutate(perc = …Run Code Online (Sandbox Code Playgroud) 我一直在使用库FsVerbalExpressions来编写一些函数.我正在努力以编程方式构建regEx.
例如,如果我有一个字符串"Int. Bus. Mach",我可以删除句点和空格,最后得到数组
let splitString = [|"Int"; "Bus"; "Mach"|]
Run Code Online (Sandbox Code Playgroud)
我想做的是构建一个正则表达式splitString,使其结果如下:
let hardCoded =
VerbEx()
|> startOfLine
|> then' "Int"
|> anything
|> whiteSpace
|> then' "Bus"
|> anything
|> whiteSpace
|> then' "Mach"
hardCoded;;
val it : VerbEx =
^(Int)(.*)\s(Bus)(.*)\s(Mach) {MatchTimeout = -00:00:00.0010000;
Regex = ^(Int)(.*)\s(Bus)(.*)\s(Mach);
RegexOptions = None;
RightToLeft = false;}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道如何以编程方式构建它,因此,如果原始字符串是"This is a much bigger string",整个regEx是从代码而不是硬编码构建的.我可以创建单独的正则表达式
let test =
splitString
|> Array.map (fun thing -> VerbEx()
|> then' thing) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将连接字符串和凭据数据存储在.config文件中.我无法使用连接/凭证将配置推送到repo; 配置将位于安全的同步文件夹中,该文件夹不是主目录.
我可以将连接/凭证存储app.config在主目录中的文件中,并使用FSharp.Configuration库访问它:
type connection = AppSettings<"app.config">
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试访问不同目录中的配置
open System.IO
open FSharp.Configuration
let baseDirectory = __SOURCE_DIRECTORY__
let baseDirectory' = Directory.GetParent(baseDirectory)
let configPath = "Tresor\app.config"
let fullConfigPath = Path.Combine(baseDirectory'.FullName, configPath)
type Settings = AppSettings<fullConfigPath>
Run Code Online (Sandbox Code Playgroud)
该fullConfigPath错误不与
This is not a valid constant expression or custom attribute value.
Run Code Online (Sandbox Code Playgroud)
即使我尝试使用yaml类型提供程序
let yamlPath = "Tresor\Config.yaml"
let fullYamlPath = Path.Combine(baseDirectory'.FullName, yamlPath)
type Config = YamlConfig<FilePath = fullYamlPath>
Run Code Online (Sandbox Code Playgroud)
我得到了类似的错误fullYamlPath.
有没有理由我无法访问主目录之外的文件?我正确构建文件路径吗?
假设我有一个示例数据框:
frame <-
data.frame(group = c(rep(1, 3), rep(2, 3)),
idea = c(1, 2, 3, 1, 2, 4),
value = c(10000, 5000, 50, 5000, 7500, 100),
level = sample(c("rough", "detailed"), 6, TRUE))
Run Code Online (Sandbox Code Playgroud)
我想要一个值的条形图,其中组内的每个想法按其值排序。我可以这样靠近
library(dplyr)
library(ggplot2)
top_ideas <-
frame %>%
group_by(group) %>%
arrange(group, desc(value))
frame %>%
group_by(group) %>%
mutate(idea = idea %>% factor(levels = top_ideas$idea)) %>%
ggplot(aes_string(x = "idea", y = "value", fill = "level")) +
geom_bar(stat = "identity") +
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 45, vjust = 1, hjust …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用dplyr管道运算符 ( %>%) 来检索存储在数据框中的模型对象。
带有示例数据
library(dplyr)
set.seed(256)
dat <-
data.frame(x = rnorm(100),
y = rnorm(100, 10),
spec = sample(c("1", "2"), 100, TRUE)) %>%
group_by(spec) %>%
do(lm = lm(y ~ x, data = .))
Run Code Online (Sandbox Code Playgroud)
我可以子集并检索实际的模型对象
> dat$lm[dat$spec == "1"][[1]]
Call:
lm(formula = y ~ x, data = .)
Coefficients:
(Intercept) x
9.8171 -0.2292
> dat$lm[dat$spec == "1"][[1]] %>% class()
[1] "lm
Run Code Online (Sandbox Code Playgroud)
但我认为这是检索lm()其中包含的模型对象的一种不优雅的方式,特别是考虑到我的代码的其余部分是按“dplyr方式”构建的。我想使用dplyr,但不知道如何使用。例如,使用
dat %>% filter(spec == "1") %>% select(lm)
Run Code Online (Sandbox Code Playgroud)
返回时不起作用
Source: local …Run Code Online (Sandbox Code Playgroud)