如何基于向量隔离数据框中的值,并使用 R 将其乘以同一数据框中的另一列?
How to isolate values in a dataframe based on a vector and multiply it by another column in the same dataframe using R?
我有一个包含多列的数据框
col1|col2|col3|colA|colB|colC|Percent
1 1 1 2 2 2 50
之前我对列进行了子集化并创建了一个向量
ColAlphabet<-c("ColA","ColB","ColC")
我想做的是将 ColAlphabet 乘以百分比,所以最后我得到了
col1|col2|col3|colA|colB|colC|Percent
1 1 1 1 1 1 50
我们可以使用 mutate
和 across
。指定用 all_of
包裹的感兴趣的列,并将这些列与 'Percent'
相乘
library(dplyr)
df2 <- df1 %>%
mutate(across(all_of(ColAlphabet), ~ .* Percent/100))
-输出
df2
# col1 col2 col3 colA colB colC Percent
#1 1 1 1 1 1 1 50
数据
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))
您可以对列进行子集化,乘以 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
我们也可以使用apply()
:
#Vector
ColAlphabet<-c("colA","colB","colC")
#Code
df[,ColAlphabet] <- apply(df[,ColAlphabet],2,function(x) x*df$Percent/100)
输出:
df
col1 col2 col3 colA colB colC Percent
1 1 1 1 1 1 1 50
使用了一些数据:
#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))
万一你想直接相乘:
> 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
>
我有一个包含多列的数据框
col1|col2|col3|colA|colB|colC|Percent
1 1 1 2 2 2 50
之前我对列进行了子集化并创建了一个向量
ColAlphabet<-c("ColA","ColB","ColC")
我想做的是将 ColAlphabet 乘以百分比,所以最后我得到了
col1|col2|col3|colA|colB|colC|Percent
1 1 1 1 1 1 50
我们可以使用 mutate
和 across
。指定用 all_of
包裹的感兴趣的列,并将这些列与 'Percent'
library(dplyr)
df2 <- df1 %>%
mutate(across(all_of(ColAlphabet), ~ .* Percent/100))
-输出
df2
# col1 col2 col3 colA colB colC Percent
#1 1 1 1 1 1 1 50
数据
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))
您可以对列进行子集化,乘以 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
我们也可以使用apply()
:
#Vector
ColAlphabet<-c("colA","colB","colC")
#Code
df[,ColAlphabet] <- apply(df[,ColAlphabet],2,function(x) x*df$Percent/100)
输出:
df
col1 col2 col3 colA colB colC Percent
1 1 1 1 1 1 1 50
使用了一些数据:
#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))
万一你想直接相乘:
> 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
>