简体   繁体   English

使用 for 循环创建新列,但我想在循环中这些新列的每个名称中添加“.Corr”。 我怎样才能做到这一点?

[英]make new columns with for loop but i want to add “.Corr” to each name of these new columns in the loop. How can i do that?

these last three column are generated from the loop.最后三列是从循环中生成的。 $PFN Date Open High Low Close Volume Adjusted Return HYG TLT SPY 1 2020-01-02 10.62 10.62 10.58 10.58 267700 10.231670 NA 0.6840342 0.043489 0.656935 2 2020-01-03 10.59 10.69 10.58 10.68 351800 10.328378 0.94518 0.6840342 0.043489 0.656935 3 2020-01-06 10.66 10.73 10.66 10.73 318400 10.376730 0.46816 0.6840342 0.043489 0.656935 4 2020-01-07 10.71 10.76 10.70 10.71 261500 10.357388 -0.18639 0.6840342 0.043489 0.656935 $PFN Date Open High Low Close Volume Adjusted Return HYG TLT SPY 1 2020-01-02 10.62 10.62 10.58 10.58 267700 10.231670 NA 0.6840342 0.043489 0.656935 2 2020-01-03 10.59 10.69 10.58 10.68 351800 10.328378 0.94518 0.6840342 0.043489 0.656935 3 2020-01- 06 10.66 10.73 10.66 10.73 318400 10.376730 0.46816 0.6840342 0.043489 0.656935 4 2020-01-07 10.71 10.71 10.76 10.76 10.70 10.71

but i want to achieve this但我想实现这个

$PFN Date Open High Low Close Volume Adjusted Return HYG.Corr TLT.Corr SPY.Corr 1 2020-01-02 10.62 10.62 10.58 10.58 267700 10.231670 NA 0.6840342 0.043489 0.656935 2 2020-01-03 10.59 10.69 10.58 10.68 351800 10.328378 0.94518 0.6840342 0.043489 0.656935 3 2020-01-06 10.66 10.73 10.66 10.73 318400 10.376730 0.46816 0.6840342 0.043489 0.656935 4 2020-01-07 10.71 10.76 10.70 10.71 261500 10.357388 -0.18639 0.6840342 0.043489 0.656935 $PFN Date Open High Low Close Volume Adjusted Return HYG.Corr TLT.Corr SPY.Corr 1 2020-01-02 10.62 10.62 10.58 10.58 267700 10.231670 NA 0.6840342 0.043489 0.656935 2 2020-01-03 10.59 10.69 10.58 10.68 351800 10.328378 0.94518 0.6840342 0.043489 0.656935 3 2020-01-06 10.66 10.73 10.66 10.73 318400 10.376730 0.46816 0.6840342 0.043489 0.656935 4 2020-01-07 10.71 10.76 10.70 10.71 261500 10.357388 -0.18639 0.6840342 0.043489 0.656935

Everything should be done in for loop!一切都应该在 for 循环中完成!

If the dataset objects are named 'PFN', 'PFP' etc, get the values of the objects, then rename those columns to preferred value and assign it back (not recommended to create multiple objects in global env).如果数据集对象被命名为“PFN”、“PFP”等, get对象的值,然后将这些列重命名为首选值并将其重新assign (不建议在全局环境中创建多个对象)。 It is better to have this in a list最好将其列在list

objs <- c('PFN', 'PFP')
 for(obj in objs){
    tmp <- get(obj)
    nm1 <- c("HYG", "TLT", "SPY")
    names(tmp)[names(tmp) %in% nm1] <- paste0(nm1, ".Corr")
    assign(obj, tmp)
 }

暂无
暂无

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

相关问题 如何为每个回归循环迭代向数据集添加新列? - How do I add new columns to a data set for each regression loop iteration? 如何使用 mutate 和 for 循环创建新列? - How do I create new columns using a mutate and for loop? 在每个for循环之后如何在数据帧中添加新列? - How do I add a new column to a dataframe after each for loop? 循环以使用ifelse添加新列 - Loop to add new columns with ifelse 向数据框添加新列,从而在循环中为每列提供不同的名称 - add new columns to a dataframe giving each column different names in a loop 如何使用 for 循环获取每一列的异常值? - How do I obtain the outliers for each of my columns using a for loop? 如何使用DPLYR创建一个新的数据框,其中两列中的变量彼此匹配? - How do I make a new data frame with variables in two columns matching each other, using DPLYR? R-循环创建DF(小齿轮)。 如何重命名它们和其中的列以包括日期? (我用eval(..)来做,但是有更好的解决方案吗?) - R - Creating DFs (tibbles) in a loop. How to rename them and columns inside, to include date? (I do it with eval(..), but is there a better solution?) 如何在R数据帧中的列之间循环并在每次迭代中使用列名创建新的数据帧? - How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration? 如何从列上的数据名称中创建新的数据框? - How do I make a new dataframe out of the data names on the columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM