如何在RStudio项目环境中使用相对路径?
例如,要访问文件,我使用整个路径:
# My RStudio project working directory:
getwd()
[1] "C:/Users/MaurizioLocale/OneDrive/Data_Science/10_Capstone_project/
CP_Natural_Language/MY_FILE.txt"
Run Code Online (Sandbox Code Playgroud)
但它真的很长.
我正在尝试使用相对于工作环境的路径.我在概念上尝试了类似于:
"~/MY_FILE.txt"
Run Code Online (Sandbox Code Playgroud)
其中,~
代表的工作环境.不幸的是,它不起作用.
使用C,我可以用通用文本编辑器(ig nano)编写程序,并在Lunix终端中编译
gcc mySum.c -o mySum
Run Code Online (Sandbox Code Playgroud)
获取一个窗口询问输入并返回输出.
#include <stdio.h>
int main(){
int x;
int y;
int sum;
printf("Program that sums two numbers.\n\nPlease type the first one:\n");
scanf("%d", &x);
printf("Type the second one:\n");
scanf("%d", &y);
sum = x + y;
printf("The sum of %d and %d is %d\n", x, y, sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在没有Visual Studio/MonoDevelopment的情况下,我可以在F#中生成相同类型的程序吗?
我发现使用裸体文本编辑器非常有启发性,就像我在使用C一样.这使我更加专注于学习,而IDE的帮助较少.此外,文本编辑器(如nano或记事本++或其他)提供了比fsharpi
编写程序更灵活的工具.通过这种方式,当程序完成时,我将它以与我在示例中使用C相同的方式将其提供给编译器.
我会说通过实现这个功能
let mySum x y =
x + y
Run Code Online (Sandbox Code Playgroud)
同
fsharpc mySum.fs
Run Code Online (Sandbox Code Playgroud)
但我没有得到如何实现它.我找到了这个参考,但它有点先进.
这个问题在 Stackoverflow 上并不新鲜,但我很确定我遗漏了一些明显的东西。
我正在尝试将一些 .pdf 文件转换为 .txt 文件,以便挖掘它们的文本。我的方法基于这个优秀的脚本。.pdf 文件中的文本不是由图像组成的,因此不需要 OCR。
# Load tm package
library(tm)
# The folder containing my PDFs
dest <- "./pdfs"
# Correctly installed xpdf from http://www.foolabs.com/xpdf/download.html
file.exists(Sys.which(c("pdfinfo", "pdftotext")))
[1] TRUE TRUE
# Delete white spaces from pdfs' names
sapply(myfiles, FUN = function(i){
file.rename(from = i, to = paste0(dirname(i), "/", gsub(" ", "", basename(i))))
})
# make a vector of PDF file names
myfiles <- list.files(path = dest, pattern = "pdf", full.names = …
Run Code Online (Sandbox Code Playgroud) 我正在学习不同数据类型的特征.例如,该程序越来越多地使用四种不同格式打印2的强大功能:integer, unsigned integer, hexadecimal, octal
#include<stdio.h>
int main(int argc, char *argv[]){
int i, val = 1;
for (i = 1; i < 35; ++i) {
printf("%15d%15u%15x%15o\n", val, val, val, val);
val *= 2;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有用.unsigned
上去2147483648
.integer
上去-2147483648
.但为什么它变得消极?
我有一个理论:是因为我们可以在32位机器上表示的最大有符号整数是2147483647
?如果是这样,为什么它会返回负数?
我是一个处理琐碎问题的F#新手.如何检查两个整数是否是共同素数?我发现这种pythonic方法非常有趣,恕我直言,优雅.我试图用F#翻译它,没有财富.我想这主要是因为我缺乏经验.
无论如何,这是迄今为止我的"最佳"尝试:
let prime (x, y) =
if y <> 0 then
(x, y) <| (y, x % y)
else
x;;
Run Code Online (Sandbox Code Playgroud)
它应该结果,即在
prime 23 13;;
- : bool = true
Run Code Online (Sandbox Code Playgroud)
显然,它不起作用.在F#中解决这个问题的最佳方法是什么?我来自R编程,需要完全不同的心态.
给定一个列表,我想生成从第一个列表中选择的第二个元素列表.
例如:
let l1 = [1..4]
let n = [0; 2]
l1.[n]
Run Code Online (Sandbox Code Playgroud)
应该返回,1
并且3
,第一和第三个元素l1
.不幸的是,它返回一个错误:
error FS0001: This expression was expected to have type
int but here has type int list
Run Code Online (Sandbox Code Playgroud)
现在,我想知道,确实存在一种方法来传递n
表示列表的参数,或者更好的是表达式?
我构建了一个简单的函数,给定一个列表,返回该列表的第一个n
元素.
let rec first l n =
match l, n with
(_, 0) -> l
| (x::xs 1) -> [x]
| (x::xs n) -> x::(first xs (n-1))
Run Code Online (Sandbox Code Playgroud)
但是,如果输入是列表而不是列表,该怎么办?我想构建一个函数,给定列表列表,返回每个列表中的第一个n
元素.例如:
first [[1; 2]; [5; 6; 7]; []; []; [9; 8; 0]] 1 =
[1; 5; 9]
Run Code Online (Sandbox Code Playgroud)
我试图通过使模式成为列表列表来找出一种方法:
let rec first l n =
match l, n with
(_, 0) -> l
| ([[x]::[xs]], n) -> [x::[first xs (n-1)]]
Run Code Online (Sandbox Code Playgroud)
它不起作用,但我更关心这种方法.这是对的吗?
如何更改Rpres中数学公式的字体大小?[编者注:'Rpres'是RStudio用于其"Presentation"文件的文件扩展名.它显然被用户采用作为IDE的那一部分的简写.] RPres中的代码框大小和字体大小面临类似的问题,但它没有解决数学公式的问题,这不是一个问题的一部分.代码与文本分离,但是是一大块LaTeX代码.
例如,请考虑使用默认设置生成的幻灯片的此部分:
The average height is 175cm, with a standard deviation of 10.
The z-score for a student 163cm is given by the formula:
$$z = (x - \mu)/\sigma$$
where $x$ is the student of our interest, $\mu$ is the distribution mean of
the school and $\sigma$.
Run Code Online (Sandbox Code Playgroud)
返回:
果然,RPres中代码框大小和字体大小中建议的方法不会影响数学公式的大小.
我仍然试图获得函数式编程的思维方式,因此我不确定这个问题是否有意义:是否可以在F#中进行作用域分配,因为它在R中?
让我们假设我设计一个功能split x
时,x = y + 10z
为abs(int:y) <= 5
和abs(int:z)
.
let split x =
let z = abs(x / 10)
let modz = abs(x % 10)
let y =
if modz > 5 then
let z = z + 1
abs(5 - modz)
else
modz
(y, z);;
Run Code Online (Sandbox Code Playgroud)
给定z大于5的模块,我想修改z的值.即,因为split 27
我期待结果(2,3).
split 22;;
val it : int * int = (2, 2)
split 27;;
val it : int * int = …
Run Code Online (Sandbox Code Playgroud) 我想创建一个产生两个输出的函数.请考虑以下示例:
我建立了两个函数,给定一个整数列表,返回偶数位置的元素列表和奇数位置的元素.
let rec alternate1 lst =
match lst with
[] -> []
| [x] -> []
| x::y::xs -> y::(alternate1 xs)
let rec alternate2 lst =
match lst with
[] -> []
| [x] -> [x]
| x::y::xs -> x::(alternate2 xs)
Run Code Online (Sandbox Code Playgroud)
这里一切都很好.现在的问题:我想创建一个单一的功能alternate
与签名返回这两个列表alternate: int list-> (int list * int list)
.
let rec alternate lst =
match lst with
[] -> []
| [x] -> []
| [x::y] -> [y]
(*My attempts:*)
| x::y::xs -> …
Run Code Online (Sandbox Code Playgroud)