简体   繁体   English

如何基于向量隔离数据帧中的值,并使用 R 将其乘以同一数据帧中的另一列?

[英]How to isolate values in a dataframe based on a vector and multiply it by another column in the same dataframe using R?

I have a dataframe with multiple columns我有一个包含多列的数据框

col1|col2|col3|colA|colB|colC|Percent
  1     1    1    2    2    2     50

Earlier I subset the columns and created a vector早些时候我对列进行了子集化并创建了一个向量

ColAlphabet<-c("ColA","ColB","ColC")

What i want to do is take ColAlphabet and multiply it by Percent so in the end I have我想要做的是将 ColAlphabet 乘以百分比,所以最后我有

col1|col2|col3|colA|colB|colC|Percent
  1    1    1     1    1    1      50

We can use mutate with across .我们可以用mutateacross Specify the columns of interest wrapped with all_of and multiply the columns with 'Percent'指定用all_of包裹的感兴趣的列并将列乘以“百分比”

library(dplyr)
df2 <- df1 %>%
     mutate(across(all_of(ColAlphabet), ~ .* Percent/100))

-output -输出

df2
#  col1 col2 col3 colA colB colC Percent
#1    1    1    1    1    1    1      50

data数据

df1 <- structure(list(col1 = 1L, col2 = 1L, col3 = 1L, colA = 2L, colB = 2L, 
    colC = 2L, Percent = 50L), class = "data.frame", row.names = c(NA, 
-1L))

You can subset the column, multiply with Percent and save it in ColAlphabet again.您可以对列进行子集化,乘以Percent并将其再次保存在ColAlphabet

ColAlphabet<-c("colA","colB","colC")
df[ColAlphabet] <- df[ColAlphabet] * df$Percent/100
df

#  col1 col2 col3 colA colB colC Percent
#1    1    1    1    1    1    1      50

We can also use apply() :我们也可以使用apply()

#Vector
ColAlphabet<-c("colA","colB","colC")
#Code
df[,ColAlphabet] <- apply(df[,ColAlphabet],2,function(x) x*df$Percent/100)

Output:输出:

df
  col1 col2 col3 colA colB colC Percent
1    1    1    1    1    1    1      50

Some data used:使用的一些数据:

#Data
df <- structure(list(col1 = 1L, col2 = 1L, col3 = 1L, colA = 2L, colB = 2L, 
    colC = 2L, Percent = 50L), class = "data.frame", row.names = c(NA, 
-1L))

In case if you want to multiply directly:如果你想直接相乘:

> df <- data.frame(col1 = 1, col2 = 1, col3 = 1, colA = 2, colB = 2, colC = 2, Percent = 50)
> df
  col1 col2 col3 colA colB colC Percent
1    1    1    1    2    2    2      50
> df[grep('^c.*[A-Z]$', names(df))] <- df[grep('^c.*[A-Z]$', names(df))] * df$Percent/100
> df
  col1 col2 col3 colA colB colC Percent
1    1    1    1    1    1    1      50
> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R Dataframe:如何根据另一列中的相应值乘以一列中的过滤值? - R Dataframe: How to multiply filtered values in a column based on the corresponding value in another column? 根据 R 中同一 dataframe 中的另一列的值将值分配给一列 - Assigning values to a column in the based on values of another column in the same dataframe in R 从 dataframe 创建一个向量,该向量根据 r 中的另一列区分值 - Creating a Vector from a dataframe that discriminates values based on another column in r 如何根据另一个向量中的值删除 R 中数据帧中的列? - How to remove columns in a dataframe in R based on the values from another vector? 基于数据帧中另一个向量中的值在r中创建向量 - Creating vector in r based on values in another vector in a dataframe 如何基于另一列的值聚合一列的R数据帧 - How to aggregate R dataframe of one column based on values of another 如何基于一个数据框中的列的值和R中另一个数据框的列标题名称有条件地创建新列 - how to conditionally create new column based on the values of a column in one dataframe and the column header names of another dataframe in R R-将一个数据框乘以另一个数据框 - R - Multiply a Dataframe by Another dataframe 将数据帧的每一列与 R 中该数据帧的另一列相乘 - multiply every column of a dataframe with another column of that dataframe in R 如何根据来自另一个数据框列的值使用 R 改变数据框的某些值 - How to mutate some values of a dataframe based on values from another dataframe column with R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM