通常,我如何根据另一个数据帧中的因子值对数据帧的一列中的值进行分类?例如,给定df1和df2我想生成df3(或更新df1):
> df1
NewAge
1 5
2 25
3 18
4 9
5 43
6 15
7 17
> df2
AgeStart AgeEnd AgeType
1 0 10 A
2 10 20 B
3 20 30 A
4 30 40 B
5 40 50 A
Run Code Online (Sandbox Code Playgroud)
我想df3为:
NewAge Type
5 A
25 A
18 B
9 A
43 A
15 B
17 B
Run Code Online (Sandbox Code Playgroud)
我使用cut()来生成间隔
df2_cut <- data.frame(NewAge,
"AgeRange" = cut(NewAge,
breaks=AgeStart,
right=F,
include.lowest=T))
> df2_cut
NewAge AgeRange
1 5 [0,10)
2 25 [20,30) …Run Code Online (Sandbox Code Playgroud) 我只是想将文件扩展名更改为 .doc。我正在尝试下面的代码,但它不起作用。怎么来的?我正在使用这里的说明
startingDir<-"C:/Data/SCRIPTS/R/TextMining/myData"
filez<-list.files(startingDir)
sapply(filez,FUN=function(eachPath){
file.rename(from=eachPath,to=sub(pattern =".LOG",replacement=".DOC",eachPath))
})
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
DD17-01.LOG DD17-02.LOG DD17-03.LOG DD17-4.LOG DD17-5.LOG DD37-01.LOG DD37-02.LOG DD39-01.LOG DD39-02.LOG DD39-03.LOG
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Run Code Online (Sandbox Code Playgroud) 我在这里错过了一些重要的东西并且看不到它。
为什么 min 和 max 不能用于设置轴限制?
mtcars %>%
select(mpg, cyl, disp, wt) %>%
filter(complete.cases(disp)) %>%
ggplot() +
geom_point(aes(x=mpg, y=disp, colour=cyl), size=3) +
xlim(min(mpg, na.rm=TRUE),max(mpg, na.rm=TRUE)) +
ylim(min(disp, na.rm=TRUE),max(disp, na.rm=TRUE)) +
scale_colour_gradient(low="red",high="green", name = "cyl")
Run Code Online (Sandbox Code Playgroud)
这有效:
mtcars %>%
select(mpg, cyl, disp, wt) %>%
filter(complete.cases(disp)) %>%
ggplot() +
geom_point(aes(x=mpg, y=disp, colour=cyl), size=3) +
# xlim(min(mpg, na.rm=TRUE),max(mpg, na.rm=TRUE)) +
# ylim(min(disp, na.rm=TRUE),max(disp, na.rm=TRUE)) +
scale_colour_gradient(low="red",high="green", name = "cyl")
Run Code Online (Sandbox Code Playgroud) 我有一个(11590 x 2)df,其中包含两个因子变量(值,ind),如下所示:
> head(df)
values ind
8632 acanthite X138
40132 acanthite X638
1 actinolite X1
1387 actinolite X23
1765 actinolite X29
1891 actinolite X31
Run Code Online (Sandbox Code Playgroud)
当我尝试获取所有唯一值时,为什么会出现以下错误?我应该如何解决这个错误,以获得仅包含唯一值记录的df?任何帮助,将不胜感激。
> unidf<-unique(df,"values")
Error: argument 'incomparables != FALSE' is not used (yet)
Run Code Online (Sandbox Code Playgroud) 我如何更改此 powershell 脚本
Start-Process –FilePath “C:\Data\PROJECTS\ABC.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
Run Code Online (Sandbox Code Playgroud)
到:
例如,我想将 ABC.pdf 的第 3、4、7 页打印成三个单独的文件,分别称为 ABC_3.png、ABC_4.png 和 ABC_7.png;图像文件可以是任何格式(.png、.jpg、.tif 等)。
我计划调用 .csv 列表来获取所有参数值(例如要打印的页码、带页码的输出名称、新文件位置的文件路径等),但我不知道如何设置 powershell 语法。谢谢你。
更新:
我使用下面的脚本在此任务上取得了进展,该脚本调用了 ghostcript。除了我似乎无法将我的 -dFirstPage 和 -dLastPage 设置为来自我的 csv 的参数之外,它执行了 1-3 项操作……我收到了 powershell 错误:
Invalid value for option -dFirstPage=$pg, use -sNAME = to define string constants
Run Code Online (Sandbox Code Playgroud)
如果我用一个数字替换 $pg ,它似乎工作正常。我将如何使用 -sNAME 来解决这个问题?
新脚本
#Path to your Ghostscript EXE
$tool = 'C:\Program Files\gs\gs9.19\bin\gswin64c.exe'
$files = Import-CSV …Run Code Online (Sandbox Code Playgroud) 我可以用来=countif()查找具有特殊字符的单元格。例如,如果我正在寻找带有“!”的单元格 我可以用=countif("*!*")。
如果我正在寻找带有“””(双引号)的单元格,我该如何执行此操作。
我尝试了以下方法,但它们不起作用:
=countif("*"*")
=countif("*~"*")
=countif(~"*"*")
Run Code Online (Sandbox Code Playgroud) 我有数百个文件,我想根据相关列表以特定方式重命名它们。我不想重复相同的 cmdlet,如下所示,我想将该 cmdlet 指向几个包含旧名称和新名称的列表文件(如下),例如 old.txt 和 new.txt。如何使用 PowerShell 执行此操作?
例子:
Rename-Item -Path "E:\MyFolder\old1.pdf" -NewName new1.pdf
Rename-Item -Path "E:\MyFolder\old2.tif" -NewName new2.pdf
Rename-Item -Path "E:\MyFolder\old3.pdf" -NewName new3.pdf
Run Code Online (Sandbox Code Playgroud)
列出文件:
旧.txt =
old1.pdf
old2.tif
old3.pdf
Run Code Online (Sandbox Code Playgroud)
新.txt =
new1.pdf
new2.tif
new3.pdf
Run Code Online (Sandbox Code Playgroud) 如何使用 ggplot2 绘制带有计数标签的“填充”条?
我可以为“堆叠”条形图执行此操作。但我很困惑,否则。
这是使用dplyr和mpg数据集的可重现示例
library(ggplot)
library(dplyr)
mpg_summ <- mpg %>%
group_by(class, drv) %>%
summarise(freq = n()) %>%
ungroup() %>%
mutate(total = sum(freq),
prop = freq/total)
g <- ggplot(mpg_summ, aes(x = class, y = prop, group = drv))
g + geom_col(aes(fill = drv)) +
geom_text(aes(label = freq), position = position_stack(vjust = .5))
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试绘制填充条的计数,则它不起作用
g <- ggplot(mpg_summ, aes(x=class, fill=drv))
g + stat_count(aes(y = (..count..)/sum(..count..)), geom="bar", position="fill") +
scale_y_continuous(labels = percent_format())
Run Code Online (Sandbox Code Playgroud)
此外,如果我尝试:
g <- ggplot(mpg_summ, aes(x=class, fill=drv))
g …Run Code Online (Sandbox Code Playgroud) 我有一个列表输出,看起来像这个片段:
[[1]]
[1] 109
[[2]]
integer(0)
[[3]]
[1] 80
Run Code Online (Sandbox Code Playgroud)
有没有办法将其转换为这种格式?
C1 C2
1 109
2 0 (or NA)
3 80
Run Code Online (Sandbox Code Playgroud)
不知道从哪里开始...如果我取消它,我注意到我失去了整数(0)位置,这对我来说很重要.
列表选择selectInput()是通过对值进行硬编码来完成的,如下例所示?selectInput:
selectInput(inputID = "variable", label ="variable:",
choices = c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"))
Run Code Online (Sandbox Code Playgroud)
但是,我希望我的列表是choices来自用户选择的列的唯一值列表,该列来自用户上传的文件(csv).我怎么能这样做?这是我得到的:
UI
shinyUI(fluidPage(
fluidRow(
fileInput('datafile', 'Choose CSV file',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
uiOutput("selectcol10"),
uiOutput("pic")
))
)
Run Code Online (Sandbox Code Playgroud)
服务器
shinyServer(function(input, output) {
filedata <- reactive({
infile <- input$datafile
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
temp<-read.csv(infile$datapath)
#return
temp[order(temp[, 1]),]
})
output$selectcol10 <- renderUI({
df <-filedata()
if (is.null(df)) return(NULL)
items=names(df)
names(items)=items
selectInput("selectcol10", "Primary C",items)
})
col10 …Run Code Online (Sandbox Code Playgroud) r ×7
ggplot2 ×2
powershell ×2
aggregate ×1
bar-chart ×1
countif ×1
dynamic ×1
excel ×1
file-rename ×1
ghostscript ×1
grouping ×1
list ×1
pdf ×1
plot ×1
printing ×1
rename ×1
shiny ×1
sorting ×1
text-mining ×1
unique ×1