在R中的特定列上应用函数

时间:2018-01-24 19:20:39

标签: r apply lapply

我有一个csv文件

此文件包含以下数据 -

category_list,Automotive & Sports,Blanks,Cleantech / Semiconductors,Entertainment,Health,Manufacturing,"News, Search and Messaging",Others,"Social, Finance, Analytics, Advertising"
,0,1,0,0,0,0,0,0,0
3D,0,0,0,0,0,1,0,0,0
3D Printing,0,0,0,0,0,1,0,0,0
3D Technology,0,0,0,0,0,1,0,0,0
Accounting,0,0,0,0,0,0,0,0,1
Active Lifestyle,0,0,0,0,1,0,0,0,0
Ad Targeting,0,0,0,0,0,0,0,0,1
Advanced Materials,0,0,0,0,0,1,0,0,0
Adventure Travel,1,0,0,0,0,0,0,0,0

将其加载到映射数据框...

mapping <- read.csv(file="mapping.csv", stringsAsFactors = FALSE,sep=",",check.names=FALSE)

数据如下(正如预期) -  enter image description here

我正在尝试在此文件中创建一个新列,其列名将针对特定行具有1。例如,对于3D行,附加列应获取“制造”的值。 只有一个&#34; 1&#34;反对每一行。

当我运行此命令时 -

mapping$sector_names <- lapply(apply(mapping[2:9], 1, function(x) which(x=="1")),names)

正确填充扇区名称列。如下图所示 -

enter image description here

问题是,当我对第2列到第10列使用apply函数时,它不起作用,在这种情况下在sector_names中获取值为NULL -

mapping$sector_names <- lapply(apply(mapping[2:10], 1, function(x) which(x=="1")),names)

enter image description here

奇怪的是,当我使用apply函数时,第3列到第10列,它工作正常......

enter image description here

简而言之 - 问题是当我在第2列到第10列应用“应用”功能时,它不起作用,但任何其他组合(2到9或3到10等)都有效。

问题是当我使用2到9时,apply函数返回列名和列号,但是当我使用2到10时只返回列号

Ex: - 输出 apply(mapping[2:9], 1, function(x) which(x=="1")) 对于每一行都是这样的......

[[2]]
Blanks 
     8

apply(mapping[2:10], 1, function(x) which(x=="1")) 对于每一行都是这样的......

[[1]] 2

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

1)如果a是问题中apply的结果,那么只需按其索引列名:

mapping$sector_names <- names(mapping)[-1][a]

2)或者将mapping1定义为mapping的0-1部分(即除第一列以外的所有部分)和nc1的矩阵是它的列数。将该矩阵乘以向量1,2,3,...将给出1的列索引向量。通过该索引向量索引mappping1的列名。这不涉及apply命令的实例。

mapping1 <- as.matrix(mapping[-1])
nc1 <- ncol(mapping1)
mapping$sector_names <- colnames(mapping1)[mapping1 %*% seq_len(nc1)]

这给出了:

> mapping$sector
[1] "Blanks"                                 
[2] "Manufacturing"                          
[3] "Manufacturing"                          
[4] "Manufacturing"                          
[5] "Social, Finance, Analytics, Advertising"
[6] "Health"                                 
[7] "Social, Finance, Analytics, Advertising"
[8] "Manufacturing"                          
[9] "Automotive & Sports"