我正在尝试创建一个使用数据框列创建表的函数:
Freq_table=function(x){
x<-factor(x)
T<-table(STI_IPD$Q19_1,x,exclude = NULL)
T<- data.frame(T)
library(reshape2)
T_x<-dcast(T, Var1~Var2)
T_x<-T_x%>%select(-starts_with("NA"),-ends_with("NA"))
}
这里STI_IPD是我的Dataframe,x应该是我用来创建另一列Q19_1的表的任何列
这是抛出错误:
FUN(X [[i]],...)出错:找不到对象'Var2'
Data.frame(T) output is:
Var1 Var2 Freq
1 Consumer Goods 1 1
2 Life Sciences 1 0
3 Chemicals 1 0
4 Other Manufacturing 1 0
5 High Tech 1 0
6 Energy 1 0
7 Mining & Metals 1 0
8 Retail & Wholesale 1 0
9 Banking/Financial Services 1 0
10 Insurance/Reinsurance 1 0
11 Services (Non-Financial) 1 0
12 Logistics 1 0
13 Other Non-Manufacturing 1 0
14 Consumer Goods <NA> 1
15 Life Sciences <NA> 1
16 Chemicals <NA> 1
17 Other Manufacturing <NA> 4
18 High Tech <NA> 1
19 Energy <NA> 5
20 Mining & Metals <NA> 0
21 Retail & Wholesale <NA> 1
22 Banking/Financial Services <NA> 5
23 Insurance/Reinsurance <NA> 3
24 Services (Non-Financial) <NA> 5
25 Logistics <NA> 2
26 Other Non-Manufacturing <NA> 3
dput(head(STI_IPD,30))的输出如下:
structure(list(Q18_1 = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_), Q19_1 = structure(c(9L, 13L, 1L, 9L,
2L, 6L, 4L, 13L, 9L, 11L, 12L, 4L, 10L, 10L, 11L, 1L, 13L, 11L,
3L, 6L, 5L, 6L, 6L, 8L, 11L, 12L, 4L, 11L, 4L, 10L), .Label = c("Consumer Goods",
"Life Sciences", "Chemicals", "Other Manufacturing", "High Tech",
"Energy", "Mining & Metals", "Retail & Wholesale", "Banking/Financial Services",
"Insurance/Reinsurance", "Services (Non-Financial)", "Logistics",
"Other Non-Manufacturing"), class = "factor"), Q46_21_4 = c(NA,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA, NA, NA), Q46_21_5 = c(NA,
NA, 1L, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, 1L,
NA, NA, NA, NA, NA, 1L, 1L, NA, 1L, NA, NA, NA, 1L), Q46_21_6 = c(NA,
NA, 1L, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_21_7 = c(NA,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L,
NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_22_4 = c(NA,
NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, 1L, 1L, NA, 1L, 1L, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, NA, 1L, NA, NA, NA), Q46_22_5 = c(1L,
1L, 1L, NA, 1L, NA, 1L, NA, NA, 1L, NA, 1L, 1L, 1L, 1L, 1L, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_22_6 = c(NA,
NA, 1L, NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, 1L, 1L, 1L, 1L, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_22_7 = c(NA,
NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_23_4 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 1L, 1L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA), Q46_23_5 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, 1L, 1L, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, 1L, 1L), Q46_23_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, 1L, 1L, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, 1L, 1L), Q46_23_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA,
NA, NA, NA, 1L, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L), Q46_24_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Q46_24_5 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA,
1L, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_24_6 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA,
1L, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_24_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
1L, NA, NA, 1L, NA, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_25_4 = c(1L,
1L, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, 1L, 1L, 1L, 1L, NA,
NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, NA, NA, NA), Q46_25_5 = c(1L,
1L, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, 1L, 1L, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, NA, NA, 1L), Q46_25_6 = c(1L,
NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, 1L, 1L, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, NA, NA, 1L), Q46_25_7 = c(1L,
NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA, 1L), Q46_26_4 = c(1L,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, 1L,
1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Q46_26_5 = c(1L,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_26_6 = c(1L,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_26_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA, 1L,
1L, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_27_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 1L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L), Q46_27_5 = c(NA,
1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_27_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_27_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_28_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA,
NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA), Q46_28_5 = c(NA,
1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, 1L), Q46_28_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, 1L), Q46_28_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA,
NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 1L), Q46_29_4 = c(NA,
NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
NA, 1L, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA), Q46_29_5 = c(NA,
1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
NA, 1L, 1L, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_29_6 = c(NA,
NA, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, 1L, NA,
NA, 1L, 1L, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_29_7 = c(NA,
NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, NA,
NA, 1L, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_30_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA), Q46_30_5 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, 1L, NA,
1L, 1L, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_30_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, 1L, NA,
1L, 1L, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_30_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA,
NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_31_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA), Q46_31_5 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L, NA,
NA, 1L, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_31_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L, NA,
NA, 1L, 1L, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_31_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
NA, 1L, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_32_4 = c(NA,
1L, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, 1L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Q46_32_5 = c(NA,
1L, 1L, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, 1L, 1L, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_32_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, 1L, 1L, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, 1L), Q46_32_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, 1L, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 1L), Q46_33_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Q46_33_5 = c(NA,
1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, NA), Q46_33_6 = c(NA,
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 1L, NA,
1L, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, NA), Q46_33_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA), Q46_34_4 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Q46_34_5 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA), Q46_34_6 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA), Q46_34_7 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Q18_1",
"Q19_1", "Q46_21_4", "Q46_21_5", "Q46_21_6", "Q46_21_7", "Q46_22_4",
"Q46_22_5", "Q46_22_6", "Q46_22_7", "Q46_23_4", "Q46_23_5", "Q46_23_6",
"Q46_23_7", "Q46_24_4", "Q46_24_5", "Q46_24_6", "Q46_24_7", "Q46_25_4",
"Q46_25_5", "Q46_25_6", "Q46_25_7", "Q46_26_4", "Q46_26_5", "Q46_26_6",
"Q46_26_7", "Q46_27_4", "Q46_27_5", "Q46_27_6", "Q46_27_7", "Q46_28_4",
"Q46_28_5", "Q46_28_6", "Q46_28_7", "Q46_29_4", "Q46_29_5", "Q46_29_6",
"Q46_29_7", "Q46_30_4", "Q46_30_5", "Q46_30_6", "Q46_30_7", "Q46_31_4",
"Q46_31_5", "Q46_31_6", "Q46_31_7", "Q46_32_4", "Q46_32_5", "Q46_32_6",
"Q46_32_7", "Q46_33_4", "Q46_33_5", "Q46_33_6", "Q46_33_7", "Q46_34_4",
"Q46_34_5", "Q46_34_6", "Q46_34_7"), class = c("data.table",
"data.frame"), row.names = c(NA, -30L), .internal.selfref = <pointer: 0x0000000000090788>)
答案 0 :(得分:0)
可能类似以下内容。
library(reshape2)
library(tidyverse)
Freq_table <- function(x){
dat <- data.frame(Q19_1 = STI_IPD$Q19_1, STI_IPD[[x]])
names(dat)[2] <- x
m <- melt(dat, id.vars = "Q19_1")
result <- tryCatch(dcast(m, Q19_1 ~ variable), error = function(e) message(e))
result <- result %>% select(-starts_with("NA"),-ends_with("NA"))
result
}
Freq_table("Q46_22_5")
Freq_table("Q46_34_4")
请注意,您将所需列的名称传递给函数,而不是列本身。
修改强>
要在评论中回答OP的请求,以下代码将上述函数应用于除输入数据帧STI_IPD
的两个第一列之外的所有列,然后将merge
所有结果应用于一个df。 Reduce/merge
代码是Hong Ooi对this question的回答。
lst <- lapply(names(STI_IPD[-(1:2)]), Freq_table)
lst <- lst[!sapply(lst, is.null)]
merge.all <- function(x, y) {
merge(x, y, all = TRUE, by = "Q19_1")
}
output <- Reduce(merge.all, lst)