使用我的数据集“chile2”,我正在进行以下计算:
Tab<-with(chile2,table(Q27,Q12_1_TEXT))
Tab<-as.data.frame.matrix(Tab)
我正在尝试下面的代码来对列进行操作,这奇怪地计算了几列而不是全部:
for(col in names(T1)){
T1[col]=(T1[col]*100)/colSums(T1)
}
for(t in names(T1)){
T1[t]=paste(round(T1[t],0),"%")
}
此后可以添加“全部”行。
还有其他更好的方法吗?
dput(chile2)
structure(list(Q27 = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("Si",
"No"), class = "factor"), Q12_1_TEXT = c("Abertis Autopistas S.a.",
"Accenture", "Adessa Falabella", "Administradora de Fondos de Cesantía",
"AES GENER S.A.", "AFP HABITAT S.A.", "Agrícola Ariztía", "Agrosuper S.A.",
"Aguas de Antofagasta S.A.", "AIG Chile Compañía de Seguros Generales S.A.",
"Arcos Dorados Restaurantes de Chile Ltda", "Artel", "Arval - Relsa",
"Banchile Inversiones", "BANCO INTERNACIONAL", "BASF CHILE SA",
"BBVA Chile", "BOEHRINGER INGELHEIM LTDA", "Bredenmaster S.A.",
"Caja de Compensación 18 de Septiembre")), class = c("data.table",
"data.frame"), row.names = c(NA, -20L), .internal.selfref = <pointer: 0x0000000000100788>, .Names = c("Q27",
"Q12_1_TEXT"))
答案 0 :(得分:1)
dcast chile2
数据以获得Q27变量的长度。然后使用prop.table
查找比例,并将其附加到&#39;%&#39;使用paste0()
library('data.table')
Tab <- dcast(chile2, Q27 ~ Q12_1_TEXT, fun.aggregate = length, value.var = 'Q12_1_TEXT')
Tab[, 2:ncol(Tab) := lapply(.SD, function(x) paste0( prop.table(x)*100, "%")), .SDcols = -1 ]
Tab
# Q27 AES GENER S.A. AFP HABITAT S.A. AIG Chile Compañía de Seguros Generales S.A. Abertis Autopistas S.a.
# 1: Si 100% 0% 100% 0%
# 2: No 0% 100% 0% 100%
现在您可以添加All
行。
修改强>
我将Q27
数据表的Tab
列从因子转换为字符,因此它是一致的。然后,我添加了Total
列,该列从第2列到最后一列采用行和。 .SDcols = -1
包括除第一列之外的所有列,因为它是字符类型。我们希望在进行行总和时使用数字列。然后我创建了ALL
行,以便稍后可以rbind
。然后计算数据中的比例,并将%
字符添加到其中。最后,使用Tab
将ALL
和rbindlist
数据合并在一起。
library('data.table')
Tab <- dcast(chile2, Q27 ~ Q12_1_TEXT, fun.aggregate = length, value.var = 'Q12_1_TEXT')
Tab[, Q27 := as.character(Q27)] # convert column one from factor to character
Tab[, Total := rowSums(.SD), .SDcols = -1 ] # add Total column
ALL <- as.list( c( Q27 = "ALL", colSums(Tab[, 2:ncol(Tab)]) ) ) # create ALL row
Tab[, 2:ncol(Tab) := lapply(.SD, function(x) paste0( prop.table(x)*100, "%")), .SDcols = -1 ] # find proportions
Tab <- rbindlist(l = list(Tab, ALL)) # row bind Tab and ALL
<强>输出:强>
str(Tab)
# Classes ‘data.table’ and 'data.frame': 3 obs. of 22 variables:
# $ Q27 : chr "Si" "No" "ALL"
# $ AES GENER S.A. : chr "100%" "0%" "1"
# $ AFP HABITAT S.A. : chr "0%" "100%" "1"
# $ AIG Chile Compañía de Seguros Generales S.A.: chr "100%" "0%" "1"
# $ Abertis Autopistas S.a. : chr "0%" "100%" "1"
# $ Accenture : chr "100%" "0%" "1"
# $ Adessa Falabella : chr "0%" "100%" "1"
# $ Administradora de Fondos de Cesantía : chr "0%" "100%" "1"
# $ Agrosuper S.A. : chr "0%" "100%" "1"
# $ Agrícola Ariztía : chr "0%" "100%" "1"
# $ Aguas de Antofagasta S.A. : chr "0%" "100%" "1"
# $ Arcos Dorados Restaurantes de Chile Ltda : chr "0%" "100%" "1"
# $ Artel : chr "100%" "0%" "1"
# $ Arval - Relsa : chr "0%" "100%" "1"
# $ BANCO INTERNACIONAL : chr "100%" "0%" "1"
# $ BASF CHILE SA : chr "0%" "100%" "1"
# $ BBVA Chile : chr "100%" "0%" "1"
# $ BOEHRINGER INGELHEIM LTDA : chr "100%" "0%" "1"
# $ Banchile Inversiones : chr "0%" "100%" "1"
# $ Bredenmaster S.A. : chr "0%" "100%" "1"
# $ Caja de Compensación 18 de Septiembre : chr "0%" "100%" "1"
# $ Total : chr "35%" "65%" "20"
# - attr(*, ".internal.selfref")=<externalptr>
答案 1 :(得分:1)
我们可以使用add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);
function my_custom_price( $price, $product )
{
//your logic for calculating the new price here
$price = $product->get_regular_price() * 0.8;
//Return the new price (this is the price that will be used everywhere in the store)
return $price;
}
和base R
与addmargins
进行此操作。从OP帖子中的prop.table
输出中,应用table
并将prop.table
指定为2(对于列式)。使用margin
创建Sum
行。然后,我们将除最后一行之外的行与100 addmargins
paste
乘以%
的值
res <- addmargins(prop.table(Tab, 2), 1)
res[-nrow(res),] <- paste0(res[-nrow(res), ] * 100, "%")
dimnames(res)[[1]][3] <- "All"
names(dimnames(res)) <- NULL
res[,1:3]
# Abertis Autopistas S.a. Accenture Adessa Falabella
#Si 0% 100% 0%
#No 100% 0% 100%
#All 1 1 1