简体   繁体   English

使用循环从 R 中的 dataframe 中的另一列创建多个列

[英]Use loop for create multiple columns from another columns in dataframe in R

I have a CSV file with 19 columns and 9 lines about average of rain months from pluviometers and coordinates (LongLat) of them.我有一个 CSV 文件,其中包含 19 列和 9 行关于雨量计和它们的坐标(LongLat)的平均降雨月份。 The columns are averages and the lines are the pluviometers.列是平均值,线条是雨量计。 The CSV file can be acessed here: https://drive.google.com/file/d/1wGVT5etZomYW-Cb6R3KgHaV4mXrTjlu4/view?usp=sharing CSV 文件可在此处访问: https://drive.google.com/file/d/1wGVT5etZomYW-Cb6R3KgHaV4mXrTjlu4/view?usp=sharing

The aim is create more columns to calculate rainfall factor by months applying a equation from average columns.目的是创建更多的列,以应用平均列的方程按月计算降雨因子。 The rainfall factor for January for each pluviometer (9 lines) will be calculate using column 7, rainfall for february using column 8, march by column 9...每个雨量计(9 行)的 1 月降雨因子将使用第 7 列计算,2 月降雨使用第 8 列计算,3 月按第 9 列计算...

I'm trying this code for each column and it works, but this code can be reduce and made with a loop?我正在为每一列尝试此代码并且它可以工作,但是可以减少此代码并使用循环制作?

library(tidyverse)
    
setwd("C:/scriptsr/R_postos_pluviometricos_interp_FUNCEME/")
        
#Code    
pluviometros <- read.csv("postos_fatorR.csv",
                                 header = T,
                                 sep = ",",
                                 stringsAsFactors = FALSE)
            
View(pluviometros)
            
pluviometros <- mutate(.data=pluviometros,R.JAN=67.355*((pluviometros[1:nrow(pluviometros),7]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.FEV=67.355*((pluviometros[1:nrow(pluviometros),8]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.MAR=67.355*((pluviometros[1:nrow(pluviometros),9]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.APR=67.355*((pluviometros[1:nrow(pluviometros),10]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.MAY=67.355*((pluviometros[1:nrow(pluviometros),11]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.JUN=67.355*((pluviometros[1:nrow(pluviometros),12]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.JUL=67.355*((pluviometros[1:nrow(pluviometros),13]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.AGO=67.355*((pluviometros[1:nrow(pluviometros),14]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.SEP=67.355*((pluviometros[1:nrow(pluviometros),15]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.OCT=67.355*((pluviometros[1:nrow(pluviometros),16]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.NOV=67.355*((pluviometros[1:nrow(pluviometros),17]^2)/pluviometros[1:nrow(pluviometros),19])^0.85,
                                       R.DEC=67.355*((pluviometros[1:nrow(pluviometros),18]^2)/pluviometros[1:nrow(pluviometros),19])^0.85
                                       )
View(pluviometros)

Here's an approach with mutate_at from dplyr :这是来自dplyrmutate_at方法:

library(dplyr)
data <- read.csv("https://docs.google.com/uc?id=1wGVT5etZomYW-Cb6R3KgHaV4mXrTjlu4&export=download")

data %>%
  mutate_at(vars(JAN:DEC),
            .funs = list(R = ~ 67.355 * ((.)^2 / TOTAL_pa)^0.85))

The only difference between yours and this is that the new columns end with _R instead of starting with R.你和这之间的唯一区别是新列以_R结尾,而不是以R. . .

If you're really excited about the names you chose, you can add rename_at :如果您对选择的名称感到非常兴奋,可以添加rename_at

library(stringr)
data %>%
  mutate_at(vars(JAN:DEC),
            .funs = list(R = ~ 67.355 * ((.)^2 / TOTAL_pa)^0.85)) %>%
  rename_at(vars(JAN_R:DEC_R),
            .funs = list(~ paste0("R.",str_remove(.,"_R"))))

Using R base there is no no need for looping, just using vectorized operations capability of R:使用 R 基础不需要循环,只需使用 R 的矢量化操作能力:

result <- 67.355 *(pluviometros[,7:18]^2 / pluviometros[,"TOTAL_pa"])^0.85 
names(result) <- paste("R", names(pluviometros)[7:18], sep=".")
pluviometros <- cbind(pluviometros, result)

Adding data.table solution:添加data.table解决方案:

library(data.table)

data <- data.table::fread("https://docs.google.com/uc?id=1wGVT5etZomYW-Cb6R3KgHaV4mXrTjlu4&export=download")

cols <- names(data)[which(names(data) == "JAN"):which(names(data) == "DEC")]

newCols <- paste0("R.", cols)

data[, (newCols) := lapply(.SD, function(x) 67.355* ((x^2) / TOTAL_pa)^0.85), .SDcols=cols]

Result:结果:

> data[, c(cols, newCols), with=F]
       JAN     FEB     MAR     APR    MAY    JUN    JUL   AGO   SEP   OCT   NOV    DEC    R.JAN
1:  85.350 126.923 177.167 177.343 89.050 18.060  7.053 1.573 0.350 1.617 2.637 14.950 492.0292
2:  96.957 130.723 173.343 184.130 89.873 25.780 10.760 0.600 0.933 0.000 0.883 13.970 592.6067
3: 106.153 170.712 212.153 198.241 96.571 35.229 21.071 3.788 1.594 5.447 6.153 16.041 592.2811
4:  89.394 120.876 181.882 149.912 89.094 20.512  8.512 0.294 0.718 2.682 0.747 20.929 543.1993
5: 103.550 107.090 139.940 141.335 66.395 18.960  5.300 0.500 0.000 0.000 3.300 17.500 776.8248
6: 106.171 150.588 167.000 154.859 64.765 18.706  7.412 0.412 3.118 0.353 1.706 25.588 714.3158
7: 102.682 141.729 187.753 176.635 88.529 21.606  3.224 0.000 0.000 0.000 1.471  6.282 651.8239
8:  79.444 103.744 155.481 134.125 53.081 16.188  1.875 0.000 0.000 0.000 0.250  6.825 535.1631
9:  87.113 103.431 128.838 145.919 60.325 25.025  1.375 0.438 0.000 0.000 0.500 11.775 612.9724
       R.FEB    R.MAR    R.APR    R.MAY    R.JUN      R.JUL      R.AGO      R.SEP      R.OCT
1:  965.9696 1702.926 1705.803 528.8379 35.10468  7.0987276 0.55383926 0.04303884 0.58043280
2:  984.8734 1591.193 1763.168 520.8968 62.33945 14.1144143 0.10433719 0.22099416 0.00000000
3: 1328.2870 1921.970 1712.663 504.2925 90.81834 37.9059855 2.04993454 0.47062304 3.80111125
4:  907.2210 1817.094 1308.148 540.1039 44.47834  9.9721478 0.03265318 0.14898636 1.39995758
5:  822.5098 1296.191 1318.234 364.9203 43.34042  4.9640584 0.08970505 0.00000000 0.00000000
6: 1293.9688 1542.759 1356.976 308.2896 37.32912  7.7369606 0.05688601 1.77529109 0.04374185
7: 1127.3785 1818.392 1639.156 506.5652 46.06450  1.8149366 0.00000000 0.00000000 0.00000000
8:  842.4019 1675.845 1303.613 269.6365 35.81053  0.9172431 0.00000000 0.00000000 0.00000000
9:  820.7410 1192.270 1473.296 328.2051 73.54207  0.5301692 0.07582386 0.00000000 0.00000000
        R.NOV     R.DEC
1: 1.33300608 25.458625
2: 0.20124041 21.999656
3: 4.67617546 23.841660
4: 0.15936024 46.026439
5: 2.21840892 37.820963
6: 0.63686082 63.583139
7: 0.47811399  5.641004
8: 0.02984560  8.248154
9: 0.09496179 20.414250

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

相关问题 For循环提取分散在另一个R数据帧中多个列上的数据 - For loop to extract data scattered across multiple columns in another R dataframe R循环根据数据框名称创建多个新列 - R loop to create multiple new columns based on dataframe name 如果多列匹配,R 从一个 dataframe 复制到另一个 - R copy from one dataframe to another if multiple columns match 从 R 数据框中的多列创建新变量的有效方法 - efficient way to create a new variable from multiple columns in R dataframe 如何从 R 中的多列创建合并值的新数据框 - How to a create a new dataframe of consolidated values from multiple columns in R 从 R 中的函数或向量在数据框中创建多列 - Create multiple columns in dataframe from a function or vector in R 在 dataframe 中创建列,R 中的字符串不存在 - create columns in dataframe with absent from string in R 从一个数据框的多个列中查找不在另一个数据列中的元素 - Finding elements from multiple columns of one dataframe that are not in multiple columns of another 如何在单个数据框中浏览两个单独的列列表,以在R中创建多个表? - How do you loop through two separate lists of columns within single dataframe to create multiple tables in R? R:数据帧中的循环对列 - R: Loop pairs of columns in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM