根据列中的值返回数据框的行。 R脚本

时间:2016-11-12 14:30:58

标签: r

我的脚本读取.xlsx工作表并使用I图表,MR图表和最后40行缺陷数量的容量图创建.pdf文件。电子表格有2列,第一列包含材料编号,第二列包含缺陷数。

我想更改脚本以便用户编写材料编号(cat命令),系统会根据各自的缺陷数量绘制图形。

示例:

用户键入数字5,1,3和8,脚本将创建一个新的表格调用“Data1”,其中包含材料编号及其各自的缺陷(1198,1201,1204,1201)。

Number  Defect
5       1198
1       1201
3       1204
8       1201

图形部分已经完成。 我尝试创建一个数据框来收集cat命令创建的数据,但我不知道如何将它与工作表相关联。我尝试使用whichsubset,但没有成功。

你能帮帮我吗?

非常感谢。

最好的问候。

更改前的代码。

cat("\f")

#install.packages(c("qcc", "openxlsx"))
library(qcc)
library(openxlsx)

qcc.options(bg.margin = "white")

#-----------------------------------------------
cat("How many sigmas do you want use for your calculus?")
NSigmas <- scan(n = 1)
`Significant algharisms`<-5

#----------------------------------------------------------------
#Control chart

Spreadsheet1 <- read.xlsx("Chord height at R42000.xlsx", sheet = "BD", 
    startRow = 1, colNames = TRUE)
Data1 <- tail(Spreadsheet1, 40)
`Chord height at R42000` <- Data1$Defect

pdf("Finish - Chord height at R42000.pdf", paper = "a4r", width = 21 / 2.54, 
    height = 29.7 / 2.54, title = "Finish - Chord height at R42000.pdf")

`I1 chart` <- qcc(`Chord height at R42000`, type = "xbar.one", nsigmas = (NSigmas/2), 
    xlab = "Observations", ylab = "Indivudual values", title = "I Chart", 
    digits = `Significant algharisms`)
`Y axis MR1` <- matrix(cbind(`Chord height at R42000`[1:length(`Chord height at R42000`)-1], 
    `Chord height at R42000`[2:length(`Chord height at R42000`)]), ncol = 2)
`MR chart` <- qcc(`Y axis MR1`, type = "R", nsigmas = (NSigmas / 2), xlab = "Observations", 
    ylab = "Moving range", title = "Moving range Chart", digits = `Significant algharisms`)
CapabilidadeI1 <- process.capability(`I1 chart`, spec.limits = c(1198, 1211), 
    nsigmas = (NSigmas / 2), target = ((1198 + 1211) / 2))

dev.off()

更改后的代码。如果您想使用“subset”ou“which”进行测试,请取消注释。

cat("\f")

#install.packages(c("qcc", "openxlsx"))
library(qcc)
library(openxlsx)

qcc.options(bg.margin = "white")

#-----------------------------------------------
cat("How many sigmas do you want use for your calculus?")
NSigmas <- scan(n = 1)
cat("What materials would you like to evaluate? Exit pressing enter twice.")
Materials<-scan()
Materials2<-data.frame(Materials)
`Significant algharisms` <- 5

#----------------------------------------------------------------
#Control chart

Spreadsheet1 <- read.xlsx("Chord height at R42000.xlsx", sheet = "BD", 
    startRow = 1, colNames = TRUE)

# Data1<-Spreadsheet1[which(Spreadsheet1[,1]==Materials2[,1]),]
# Data1<-subset(Spreadsheet1, Spreadsheet1$Number==Materials2$Materials)

## The variable don't store all numbers I type with "cat" command.

`Chord height at R42000` <- Data1$Defect

pdf("Finish - Chord height at R42000.pdf", paper = "a4r", width = 21 / 2.54, 
    height = 29.7 / 2.54, title = "Finish - Chord height at R42000.pdf")

`I1 chart` <- qcc(`Chord height at R42000`, type = "xbar.one", nsigmas = (NSigmas/2), 
    xlab = "Observations", ylab = "Indivudual values", title = "I Chart", 
    digits = `Significant algharisms`)
`Y axis MR1` <- matrix(cbind(`Chord height at R42000`[1:length(`Chord height at R42000`)-1], 
    `Chord height at R42000`[2:length(`Chord height at R42000`)]), ncol = 2)
`MR chart` <- qcc(`Y axis MR1`, type = "R", nsigmas = (NSigmas / 2), xlab = "Observations", 
    ylab = "Moving range", title = "Moving range Chart", digits = `Significant algharisms`)
CapabilidadeI1 <- process.capability(`I1 chart`, spec.limits = c(1198, 1211), 
    nsigmas = (NSigmas / 2), target = ((1198 + 1211) / 2))

dev.off()

Spreadsheet1 - 将其重命名为“完成 - R42000处的和弦高度”。

Number  Defect
2       1204
5       1198
9       1205
1       1201
4       1198
3       1204
6       1207
8       1201
10      1197
11      1203
12      1199
13      1204
14      1204
19      1208
22      1197
28      1202
30      1209
24      1205
21      1202
25      1202
20      1204
26      1200
65      1205

1 个答案:

答案 0 :(得分:0)

dt=fread("Number  Defect
2       1204
5       1198
9       1205
1       1201
4       1198
3       1204
6       1207
8       1201
10      1197
11      1203
12      1199
13      1204
14      1204
19      1208
22      1197
28      1202
30      1209
24      1205
21      1202
25      1202
20      1204
26      1200
65       1205")

d = scan(n=5)
1: 3
2: 7
3: 11
4: 65
5: 20
Read 5 items
> d
[1]  3  7 11 65 20

dt[which(dt$Number %in% d),]
   Number Defect
1:      3   1204
2:     11   1203
3:     20   1204
4:     65   1205

&#39; 7&#39;没有相应的缺陷,所以没有出现