如何为每一列重复计算? [英] How to repeat a computation for every column?
问题描述
我有一个类似于以下内容的数据集:
I have a dataset which looks like the following:
V1 V2 V3 ...
1 1 2 ...
1 1 2 ...
2 3 NA...
NA 2 1 ...
2 1 3 ...
它包含2535列,我想对所有列使用以下索引.我已经设法计算出第一列的索引.
it contains 2535 columns and I want to use the following index for all columns. I've managed to compute the index for the first column.
((max(table(df$V1)))-0.5*
((table(df$V1)["1"]+table(df$V1)["2"]+table(df$V1)["3"])
- (max(table(df$V1)))))/(table(df$V1)["1"]+table(df$V1)["2"]+table(df$V1)["3"])
有没有一种方法可以计算每2535列的索引?我曾考虑过使用循环,但没有设法写一个循环...
Is there a way how to compute the index for every 2535 of the columns? I thought about using a loop but didn't manage to write one...
推荐答案
您应该查看tidyverse软件包dplyr,特别是mutate_at
或mutate_if
.
You should have a look at the tidyverse package dplyr, specifically mutate_at
or mutate_if
.
您的职能是什么?即您想对每列执行什么操作?
What is your function? i.e. what is the operation you want to do on every column?
如果要对数据中的所有数字列执行此操作:
If you want to do this on ALL numeric columns in the data:
library(tidyverse)
mutate_if(data, is.numeric, your_function)
data = data.frame(V1 = sample(1:5, 10, replace = T),
V2 = sample(1:5, 10, replace = T),
V3 = sample(1:5, 10, replace = T))
mutate_if(data, is.numeric, mean) %>% unique()
V1 V2 V3
1 2.9 2.4 2.7
这篇关于如何为每一列重复计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!