我有这样的数据
df <- structure(list(V1 = structure(c(2L, 4L, 4L, 2L, 4L, 4L, 4L, 4L,
1L, 1L, 1L, 1L, 3L, 2L, 2L, 4L, NA, 2L, 2L, 2L, 2L, 4L, 5L, 5L,
5L, NA, NA, 4L), .Label = c("", "1 x Bruit (U)", "1 x TAMAN (M)",
"2 x Bruit (U)", "2 x TIKIam(T)"), class = "factor"), V2 = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("BUX1_T10963",
"BUX1_T10964", "BUX1_T10965", "BUX1_T10966", "BUX2_T10076"), class = "factor")), .Names = c("V1",
"V2"), class = "data.frame", row.names = c(NA, -28L))
我试着做一个例子,这样可以帮助我更好
前两列看起来像这样
1 x Bruit(U)BUX1_T10963 2 x Bruit(U)BUX1_T10963 2 x Bruit(U)BUX1_T10963
在这里,我想知道有多少BUX1_T10963有Bruit (U)
以及有多少人没有 BUX1_T10963
Bruit (U) 5
NA 0
. 0
. 0
。或者有其他东西或空单元
例如在上面的示例中,答案是
BUX1_T10963有5个Bruit(U)
输出如下所示
table(df$V1, df$V2)
BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
0 4 0 0 0
1 x Bruit (U) 1 1 2 4 0
1 x TAMAN (M) 0 0 1 0 0
2 x Bruit (U) 2 4 1 1 1
2 x TIKIam(T) 0 0 0 3 0
@ d.b给出了一个像
这样的答案table(replace(gsub("(\\d x )", "", df$V1), is.na(df$V1), "NA"), df$V2)
BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
0 4 0 0 0
Bruit (U) 3 5 3 5 1
NA 0 0 1 2 0
TAMAN (M) 0 0 1 0 0
TIKIam(T) 0 0 0 3 0
BUX1_T10963 should have 4 empty
BUX1_T10965 should have 1
BUX1_T10966 should have 2
但是它没有显示像上面这样的格式分配了多少空,也计算了2个不同于1的空白
Dim ystrdycrq As Excel.Workbook
Dim ystrdextr As Excel.Worksheet
Dim ystrdname As String = [String].Format("D:\data\données station\crq{0}.xls", DateTime.Now.AddDays(-1).ToString("ddMMyyyy"))
Dim fichiextreme As String = "K_" & Date.Now.AddDays(-1).ToString("MMdd") & ".xls"
Dim sourcextreme As String = "\\xxxxxxx\aero_mes\piste0\" & fichiextreme
Dim localextreme As String = "D:\data\bdcrq\" & fichiextreme
MsgBox(fichiextreme & sourcextreme & localextreme)
If System.IO.File.Exists(sourcextreme) Then
File.Copy("\\xxxxxx\aero_mes\piste0\" & fichiextreme, "D:\data\bdcrq\" & fichiextreme, True)
Dim extrwb As Excel.Workbook
Dim extrwsh As Excel.Worksheet
ystrdycrq = app.Workbooks.Open(ystrdname)
ystrdextr = ystrdycrq.Worksheets("extrèmes")
extrwb = app.Workbooks.Open(localextreme) ' ne pas oublier de la fermer
extrwsh = extrwb.Sheets(1)
extrwsh.Cells(1, 14).Copy(ystrdextr.Cells(1, 1))
extrwsh.Cells(2, 14).Copy(ystrdextr.Cells(2, 1))
extrwsh.Cells(1, 15).Copy(ystrdextr.Cells(1, 2))
extrwsh.Cells(2, 15).Copy(ystrdextr.Cells(2, 2))
extrwsh.Cells(1, 16).Copy(ystrdextr.Cells(1, 3))
extrwsh.Cells(2, 16).Copy(ystrdextr.Cells(2, 3))
extrwsh.Cells(1, 17).Copy(ystrdextr.Cells(1, 4))
extrwsh.Cells(2, 17).Copy(ystrdextr.Cells(2, 4))
extrwsh.Cells(1, 54).Copy(ystrdextr.Cells(1, 5))
extrwsh.Cells(2, 54).Copy(ystrdextr.Cells(2, 5))
extrwsh.Cells(1, 55).Copy(ystrdextr.Cells(1, 6))
extrwsh.Cells(2, 55).Copy(ystrdextr.Cells(2, 6))
txtUmin.Text = ystrdextr.Cells(2, 5).value
txtHH_Umin.Text = ystrdextr.Cells(2, 6).value
txtUmax.Text = ystrdextr.Cells(2, 7).value
txtHH_Umax.Text = ystrdextr.Cells(2, 8).value
txtTmin.Text = ystrdextr.Cells(2, 1).value
txtHH_Tmin.Text = ystrdextr.Cells(2, 2).value
txtTmax.Text = ystrdextr.Cells(2, 3).value
txtHH_Tmax.Text = ystrdextr.Cells(2, 4).value
extrwb.Close(SaveChanges:=False)
ystrdycrq.Close(SaveChanges:=True)
ystrdextr = Nothing
ystrdycrq = Nothing
extrwb = Nothing
extrwsh = Nothing
releaseObject(ystrdextr)
releaseObject(ystrdycrq)
releaseObject(extrwsh)
releaseObject(extrwb)
Else
MsgBox("the file .....", vbOKOnly)
End If
有没有办法让它在同一个原始产品上?
答案 0 :(得分:1)
table
命令执行您想要的操作:
table(df$V1, df$V2, useNA = "ifany")
表将适用于所有不同的值。如果您希望空白""
等同于缺失值NA
,则需要对数据进行更改:
df[df == ""] = NA
同样,如果1 x
或2 x
并不重要,请将其删除。也许添加一个新列
df$goodname = gsub(pattern = "^[0-9]+ x ", replacement = "", x = df$V1)
table(df$goodname, df$V2, useNA = "ifany")
BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
Bruit (U) 3 5 3 5 1
TAMAN (M) 0 0 1 0 0
TIKIam(T) 0 0 0 3 0
<NA> 0 4 1 2 0
将数量拉出到自己的列中并制表:
library(stringr)
# extract the number
df$quantity = as.numeric(str_extract(df$V1, "^[0-9]+"))
# any missing values assume to be 1
df$quantity[is.na(df$quantity)] = 1
library(reshape2)
dcast(data = df, formula = goodname ~ V2, value.var = "quantity", fun.aggregate = sum, na.rm = T)
# goodname BUX1_T10963 BUX1_T10964 BUX1_T10965 BUX1_T10966 BUX2_T10076
# 1 Bruit (U) 5 9 4 6 2
# 2 TAMAN (M) 0 0 1 0 0
# 3 TIKIam(T) 0 0 0 6 0
# 4 <NA> 0 4 1 2 0