0
unique(property$Agent)
unq_agent = unique(property$Agent)
lix <- c()
for (i in unq_agent) {
  j = sum(property$Sold[property$Agent==i])*0.02
  x <- c(i = j)
  lix <- c(lix, x)
}
print(lix)

     i      i      i      i      i      i      i 
347.62  25.60 338.60  13.50  14.80  84.82  92.40 
     i      i      i      i      i      i      i 
 27.50 218.82  19.10  79.26  95.40  35.60 101.22 
     i      i      i      i 
 22.00  17.22  26.30  16.24 

If I say x <- c(i) instead x <- c(i = j),

 [1] "RayWhite"              
 [2] "Belle"                 
 [3] "Raine&Horne"           
 [4] "TraversGray"           
 [5] "HarrisPartners"        
 [6] "R&W"                   
 [7] "McGrath"               
 [8] "BresicWhitney"         
 [9] "Urbane"                
[10] "GreenSquareResidential"
[11] "EeRealEstate"          
[12] "Viewey"                
[13] "GerberProperties"      
[14] "LJHooker"              
[15] "Martin"                
[16] "OneAgency"             
[17] "Iskandar"              
[18] "PlanetProperties"    

Can you give me solution, please?? I want that i can follow that for loop. So, RayWhite, Belle, Raine&Horne, ... instead i on my first result.

  • 1
    If you're interested in a better way to do this, I'd suggest the [FAQ on how to sum by group](https://stackoverflow.com/q/1660124/903061). In base R, this is a one-liner, `lix = tapply(X = property$Sold, INDEX = property$Agent, FUN = sum) * 0.02`. Or keeping in data frames, you can use `library(dplyr); property %>% group_by(Agent) %>% summarize(result = sum(Sold) * 0.02)`. – Gregor Thomas Mar 24 '21 at 03:06

1 Answers1

0

Use names(x)=i after x <- c(i = j).

Vons
  • 3,277
  • 2
  • 16
  • 19