[英]R : How to detokenize a variable inside another aggregate function?
I have a data set in which records of one particular column has to be retrieved, corresponding to each levels of the 4 categorical columns.我有一个数据集,其中必须检索一个特定列的记录,对应于 4 个分类列的每个级别。
Sample Data set: Sample.df -> (serial numbers are only for tabular view)样本数据集:Sample.df ->(序列号仅用于表格视图)
1. A B E
2. Y 1 123
3. N 0 345
4. Y 1 541
5. Y 1 567
str(Sample.df)--> str(Sample.df)-->
1. $ A : Factor w/ 2 levels "N","Y"
2. $ B : Factor w/ 2 levels 0,1
3. $ E : int 123 345 541 567
Sample desired output with values of column E:使用 E 列的值对所需的 output 进行采样:
1. Col A with Level Y
123
541
567
2. Col B with level 0
345
I wrote a for loop for extracting all values of column E我写了一个 for 循环来提取列 E 的所有值
for(i in 1:3) {
levels_check <- levels(Sample.df[,i])
if( !is.null(levels_check)){
temp_level <- levels(Sample.df[,i])
level_1 <- as.name(temp_level[1])
level_2 <- as.name(temp_level[2])
x[i] <- mean(Sample.df[which(Sample.df$as.name(names(Sample.df[i]))==as.name(level_1),names(Sample.df) %in% c("E"))])
i <- i+1
}
}
Looking for a solution to detokenize a variable value in a statement for generic loop flow.在通用循环流的语句中寻找对变量值进行去标记的解决方案。
Something like this can produce what you want.像这样的东西可以产生你想要的东西。
for (i in seq_along(levels(df$A))) {
sub_df <- split(df, df$A)
print(names(sub_df[i]))
print(sub_df[[i]]["E"])
#if you don't want to pass "E" manually, and you know it is the last col
#print(df_sub[[i]][ncol(df_sub[[i]])])
}
# [1] "N"
# E
# 2 345
# [1] "Y"
# E
# 1 123
# 3 541
# 4 567
You can ?split
if you want to see its Usage
.如果您想查看它的
Usage
,您可以?split
。
#dput(df)
structure(list(A = structure(c(2L, 1L, 2L, 2L), .Label = c("N",
"Y"), class = "factor"), B = structure(c(2L, 1L, 2L, 2L), .Label = c("0",
"1"), class = "factor"), E = c(123L, 345L, 541L, 567L)), row.names = c(NA,
-4L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.