简体   繁体   English

将列值除以R中多个列的唯一数目

[英]Dividing a column value by unique number of multiple columns in R

I need to obtain the value in the Area and Prm columns by dividing the values by the unique number of columns (YEAR DIV POL ST CTY CR PL YID LID DATE) . 我需要通过将值除以唯一的列数来获得Area和Prm列中的值(YEAR DIV POL ST CTY CR PL YID LID DATE)

Is there a function in R to achieve this? R中是否有实现此目的的功能?

Thanks in advance. 提前致谢。

Table:

Tag  YEAR  DIV  POL  ST  CTY   CR  PL  YID  LID DATE    Area PRm  SEP1  SEP2
S25  2005  7    3068 15  205   11  44  4    2   9042004 799  4504 326.9 296.6 
S1   2005  7    4077 15  205   11  90  4    2   9202004 300  3000 316.1 309.2
S16  2005  7    4077 15  205   11  90  4    2   9202004 300  3000 391.2 201.5
S2   2005  7    4077 15  205   11  90  4    2   9202004 300  3000 271.2 311.5
S28  2005  7    3180 15  205   11  44  5    6   9202004 651  1747 251.2 382.5


Output:

Tag  YEAR  DIV  POL  ST  CTY   CR  PL  YID  LID DATE    Area PRm  SEP1  SEP2
S25  2005  7    3068 15  205   11  44  4    2   9042004 799  4504 326.9 296.6 
S1   2005  7    4077 15  205   11  90  4    2   9202004 100  1000 316.1 309.2
S16  2005  7    4077 15  205   11  90  4    2   9202004 100  1000 391.2 201.5
S2   2005  7    4077 15  205   11  90  4    2   9202004 100  1000 271.2 311.5
S28  2005  7    3180 15  205   11  44  5    6   9202004 651  1747 251.2 382.5

The difference in Table and Output is in the 2,3 and 4 rows in Area and PRm columns. 表和输出的区别在于Area和PRm列的2,3和4行。

The values in the Area and Prm column (300 and 3000) was divided by 3 (because YEAR DIV POL ST CTY CR PL YID LID DATE columns are identical in rows 2,3 and 4). Area和Prm列中的值(300和3000)除以3(因为YEAR DIV POL ST CTY CR PL YID LID DATE列在第2,3和4行中相同)。 SO the values in the Area and PRm columns are divided by 3 (300 / 3 = 100 in Area column and 3000 / 3 = 1000 in PRm column in 2,3 and 4th rows in the output table. 因此,Area和PRm列中的值除以3(在输出表的第2、3和4行中,Area列中的300/3 = 100,PRm列中的3000/3 = 1000。

     S1   2005  7    4077 15  205   11  90  4    2   9202004 100  1000 316.1 309.2
    S16  2005  7    4077 15  205   11  90  4    2   9202004 100  1000 391.2 201.5
    S2   2005  7    4077 15  205   11  90  4    2   9202004 100  1000 271.2 311.5

with dplyr : dplyr

df1 %>% add_count(YEAR,DIV,POL,ST,CTY,CR,PL,YID,LID,DATE) %>%
  mutate(Area = Area /n) %>%
  select(-n)

output 输出

# # A tibble: 5 x 15
#     Tag  YEAR   DIV   POL    ST   CTY    CR    PL   YID   LID    DATE  Area   PRm  SEP1  SEP2
#   <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int>   <int> <dbl> <int> <dbl> <dbl>
# 1   S25  2005     7  3068    15   205    11    44     4     2 9042004   799  4504 326.9 296.6
# 2    S1  2005     7  4077    15   205    11    90     4     2 9202004   100  3000 316.1 309.2
# 3   S16  2005     7  4077    15   205    11    90     4     2 9202004   100  3000 391.2 201.5
# 4    S2  2005     7  4077    15   205    11    90     4     2 9202004   100  3000 271.2 311.5
# 5   S28  2005     7  3180    15   205    11    44     5     6 9202004   651  1747 251.2 382.5

data 数据

df1 <- read.table(text="Tag  YEAR  DIV  POL  ST  CTY   CR  PL  YID  LID DATE    Area PRm  SEP1  SEP2
S25  2005  7    3068 15  205   11  44  4    2   9042004 799  4504 326.9 296.6 
                  S1   2005  7    4077 15  205   11  90  4    2   9202004 300  3000 316.1 309.2
                  S16  2005  7    4077 15  205   11  90  4    2   9202004 300  3000 391.2 201.5
                  S2   2005  7    4077 15  205   11  90  4    2   9202004 300  3000 271.2 311.5
                  S28  2005  7    3180 15  205   11  44  5    6   9202004 651  1747 251.2 382.5",header=T,stringsAsFactors=F)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM