0

I would like to change the name of attributes within a column that are sampled from a loop. In the first for loop, I am taking a sample from my entire dataset and in the 2nd for loop, placing this sample in an empty dataframe that I created. Everything functions as I need it except the renaming code. My renaming solution almost works, but I need the rename in sequential order from 1 to 9.

With dummy data:

AllFruit <- read.table("AllFruitData.txt")

for (i in 1:1000) {

Fruit.sample <- sample(AllFruit$Weight, size = 9, replace = TRUE) #create a sample based on weight of fruit

WeightSample <- NULL #create an empty dataframe

for (k in 1:length(Fruit.sample)) {

WeightSample = rbind(WeightSample, AllFruit[which(AllFruit$Weight == Fruit.sample[k]),])
}
}

What this yields:

WeightSample

Type    Color   Weight 
Apple   Red     5
Banana  Yellow  5
Apple   Red     5
Apple   Red     9
Banana  Yellow  9
Orange  Orange  7
Pear    Green   3
Banana  Yellow  8
Pear    Green   6
Pear    Green   2
Banana  Yellow  1
Pear    Green   1
Pear    Green   4

What I need:

Type    Color   Weight 
Apple   Red     1
Banana  Yellow  1
Apple   Red     1
Apple   Red     2
Banana  Yellow  2
Orange  Orange  3
Pear    Green   4
Banana  Yellow  5
Pear    Green   6
Pear    Green   7
Banana  Yellow  8
Pear    Green   8
Pear    Green   9

To rename the weights in my Weight column, I have deduced that I need to change the right side of the arrow because when I add this into the 2nd loop after the rbind line:

WeightSample[WeightSample$Weight == Fruit.sample[k], "Weight"] <- 13

I get:

Type    Color   Weight 
Apple   Red     13
Banana  Yellow  13
Apple   Red     13
Apple   Red     13
Banana  Yellow  13
Orange  Orange  13
Pear    Green   13
Banana  Yellow  13
Pear    Green   13
Pear    Green   13
Banana  Yellow  13
Pear    Green   13
Pear    Green   13

I have tried a few options such as seq(1:9) but get a replacement error. Since I am sampling 1000 times, I need a solution that will account for the weight of each new sample. Thank you!

bruss
  • 21
  • 3
  • Hi bruss! Could you use `dput(AllFruit)` and copy paste the output into your question? This will allow people to easily load in the exact same data you used & more easily answer your question. See this link for more detail: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – verybadatthis Mar 30 '21 at 01:27

1 Answers1

1

We can use

WeightSample$Weight <- with(WeightSample, match(Weight, unique(Weight)))

-output

 WeightSample
     Type  Color Weight
1   Apple    Red      1
2  Banana Yellow      1
3   Apple    Red      1
4   Apple    Red      2
5  Banana Yellow      2
6  Orange Orange      3
7    Pear  Green      4
8  Banana Yellow      5
9    Pear  Green      6
10   Pear  Green      7
11 Banana Yellow      8
12   Pear  Green      8
13   Pear  Green      9

data

WeightSample <- structure(list(Type = c("Apple", "Banana", "Apple", "Apple", 
"Banana", "Orange", "Pear", "Banana", "Pear", "Pear", "Banana", 
"Pear", "Pear"), Color = c("Red", "Yellow", "Red", "Red", "Yellow", 
"Orange", "Green", "Yellow", "Green", "Green", "Yellow", "Green", 
"Green"), Weight = c(5L, 5L, 5L, 9L, 9L, 7L, 3L, 8L, 6L, 2L, 
1L, 1L, 4L)), class = "data.frame", row.names = c(NA, -13L))
akrun
  • 874,273
  • 37
  • 540
  • 662