1
降價報告句子如何生成這樣的句子:使用lapply產生從動態數據
Type A accounted for 10 (34%), type B for 8 (28%), and type C for 7 (24%), and type AB for 5 (17%).
從動態數據是這樣的:
Type <- c("A","A","A","A","A","A","A","A","A",
"B","B","B","B","B","B","B","B",
"C","C","C","C","C","C","C",
"AB","AB","AB","AB","AB")
Type <- as.data.frame(Type)
我是新來lapply函數應用於從唯一變量生成的列表,但我有點卡住了這個。
library(dplyr)
Type_list <- function(data, type) {
data %>%
filter(Type == type) %>%
paste(type, length(Type$Type[Type$Type == x])) %>%
paste0(((length(Type$Type[Type$Type == x]))/length(Type$Type)*100), "%")
}
i <- unique(Type$Type)
lapply(i, function(x) Type_list(Type, x))
如果if語句比lapply好?
那些「如何將這些數值插入到這些數據中: 」A型佔10%(34%),B型佔8%(28%),C型佔7%(24%), AB型爲5(17%)「。 – SCDCE
@Seth剛剛更新了該效果的答案。 –
是的,只是看如何顯示計數。謝謝。 – SCDCE