如何循环R中包含变量的列名?
我有一个数据帧df:如何循环R中包含变量的列名?,r,R,我有一个数据帧df: Shares Price1 Price2 Price3 100 9 10 11 200 5 6 7 300 3 2 1 我想循环这个数据帧并创建三个新列,它们等于Shares x Price[I],其中(I为1:3)。我尝试了以下代码: for (j in 1:3) { df$paste0("MktCap",j,sep="
Shares Price1 Price2 Price3
100 9 10 11
200 5 6 7
300 3 2 1
我想循环这个数据帧并创建三个新列,它们等于Shares x Price[I],其中(I为1:3)。我尝试了以下代码:
for (j in 1:3) {
df$paste0("MktCap",j,sep="")<-df$Shares*df$paste0("Price",j,sep="")
}
我已经看过了,但这并不是我想要的,因为我希望我的新列名可以重复使用。这就是你想要的吗?另外,请检查此处的链接
for(1:3中的j){
df[,paste0(“MktCap”,j,sep=”“)]df
股票价格1价格2价格3 MKTCP1 MKTCP2 MKTCP3
1 100 9 10 11 900 1000 1100
2 200 5 6 7 1000 1200 1400
3 300 3 2 1 900 600 300
这是您想要的吗?另外,请查看此处的链接
for(1:3中的j){
df[,paste0(“MktCap”,j,sep=”“)]df
股票价格1价格2价格3 MKTCP1 MKTCP2 MKTCP3
1 100 9 10 11 900 1000 1100
2 200 5 6 7 1000 1200 1400
3 300 3 2 1 900 600 300
@Wen的解决方案很有效,如果你有很多价格栏,那就是最好的选择。但我认为使用dplyr
你会得到一个更具表现力的解决方案,更容易阅读和理解:
library(dplyr)
df <- data.frame(Shares = c(100, 200, 300), Price1 = c(9, 5, 3), Price2 = c(10, 6, 2), Price3 = c(11, 7, 1))
(df <- df %>%
mutate(MktCap1 = Shares * Price1,
MktCap2 = Shares * Price2,
MktCap3 = Shares * Price3))
Shares Price1 Price2 Price3 MktCap1 MktCap2 MktCap3
1 100 9 10 11 900 1000 1100
2 200 5 6 7 1000 1200 1400
3 300 3 2 1 900 600 300
库(dplyr)
df@Wen的解决方案很有效,如果你有很多价格栏,那就是最好的选择。但我认为使用dplyr
你会得到一个更具表现力的解决方案,更容易阅读和理解:
library(dplyr)
df <- data.frame(Shares = c(100, 200, 300), Price1 = c(9, 5, 3), Price2 = c(10, 6, 2), Price3 = c(11, 7, 1))
(df <- df %>%
mutate(MktCap1 = Shares * Price1,
MktCap2 = Shares * Price2,
MktCap3 = Shares * Price3))
Shares Price1 Price2 Price3 MktCap1 MktCap2 MktCap3
1 100 9 10 11 900 1000 1100
2 200 5 6 7 1000 1200 1400
3 300 3 2 1 900 600 300
库(dplyr)
df考虑数据帧,df:
df = tribble(
~Shares, ~Price1, ~Price2, ~Price3,
100, 9, 10, 11,
200, 5, 6, 7,
300, 3, 2, 1
)
第一种方法-糟糕透了。硬编码。这是可行的,但你需要一个可复制的解决方案
df$Value1 = df$Shares * df$Price1
df$Value2 = df$Shares * df$Price2
df$Value3 = df$Shares * df$Price3
第二种方法更好,但仍然不太好。将原始数据框子集为值,乘以价格,分配列名,将数据合并在一起
stockPrice = df[,2:4]
stockValue = df$Shares * stockPrice
colnames(stockValue) = c(paste("value", seq(1:3), sep = ""))
cbind(df, stockValue)
第三种(最佳)方法-定义一个函数
calculateValues = function(df){
N = ncol(df)
L = N-1
stockPrice = df[,2:N]
stockValue = df$Shares * stockPrice
colnames(stockValue) = c(paste("value", seq(1:L), sep = ""))
cbind(df, stockValue)
}
calculateValues(df)
这将输出一个新的数据框,每次都有shares*值,命名和所有内容!唯一的问题是每次df的第一列都必须命名为“shares”。考虑数据框,df:
df = tribble(
~Shares, ~Price1, ~Price2, ~Price3,
100, 9, 10, 11,
200, 5, 6, 7,
300, 3, 2, 1
)
第一种方法-糟糕透了。硬编码。这是可行的,但你需要一个可复制的解决方案
df$Value1 = df$Shares * df$Price1
df$Value2 = df$Shares * df$Price2
df$Value3 = df$Shares * df$Price3
第二种方法更好,但仍然不太好。将原始数据框子集为值,乘以价格,分配列名,将数据合并在一起
stockPrice = df[,2:4]
stockValue = df$Shares * stockPrice
colnames(stockValue) = c(paste("value", seq(1:3), sep = ""))
cbind(df, stockValue)
第三种(最佳)方法-定义一个函数
calculateValues = function(df){
N = ncol(df)
L = N-1
stockPrice = df[,2:N]
stockValue = df$Shares * stockPrice
colnames(stockValue) = c(paste("value", seq(1:L), sep = ""))
cbind(df, stockValue)
}
calculateValues(df)
这将输出一个新的数据帧,每次都有shares*值,并命名为所有内容!唯一需要注意的是,df的第一列每次都必须命名为“shares”。您的预期输出是什么