-1

In the iris dataset, I want to replace the names setosa virginica and versicolor with a letter, then convert it to an A for example setosa with a while loop and put another letter A next to it.

so setosa = AA

virginica = BB

versicolor = CC

The code I tried only adds to the numbers, I need to do it to the letters

data(iris) 
head(iris) 
Burak <- iris

running_index <- 1

  while(is.numeric(iris[ , running_index])) { 
    iris[ ,running_index] <- iris[ ,running_index] + 50 
    running_index <- running_index + 1
  }
zephryl
  • 14,633
  • 3
  • 11
  • 30

2 Answers2

2

Is this what you’re trying to do? I really don’t follow the logic of your while loop or how it relates to your stated goal.

new_labels <- c(setosa = "AA", virginica = "BB", versicolor = "CC")

iris$Species <- new_labels[iris$Species]

iris[c(1, 61, 121), ]
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1            5.1         3.5          1.4         0.2      AA
61           5.0         2.0          3.5         1.0      BB
121          6.9         3.2          5.7         2.3      CC
zephryl
  • 14,633
  • 3
  • 11
  • 30
  • I'm trying to replace them with while loop. as in the sample code – Halil Burak Yıldırım Dec 16 '22 at 14:41
  • That doesn’t really make sense, and even if it did would be a very inefficient way of doing it; in R, most operations are vectorized, meaning you should transform vectors / columns “all at once” in one operation rather than using a loop. – zephryl Dec 16 '22 at 14:44
  • I try to learn R studio and sorry for my english.I have difficulty expressing myself. So, is there a way to loop the letters with the while command? – Halil Burak Yıldırım Dec 16 '22 at 14:49
  • I’m sorry but I really just don’t understand. Could you edit your original question to provide more explanation of what you want, including an example of your desired output? Also look at [this thread](https://stackoverflow.com/q/5963269/17303805) to learn more about asking an effective R question. – zephryl Dec 16 '22 at 14:51
0

you can try the simple below code without using loop!

data("iris")
iris$Species<-c(rep("AA", 50), rep("BB",50),
                rep ("CC", 50))
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2      AA
2          4.9         3.0          1.4         0.2      AA
3          4.7         3.2          1.3         0.2      AA
4          4.6         3.1          1.5         0.2      AA
5          5.0         3.6          1.4         0.2      AA
6          5.4         3.9          1.7         0.4      AA

tail(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
145          6.7         3.3          5.7         2.5      CC
146          6.7         3.0          5.2         2.3      CC
147          6.3         2.5          5.0         1.9      CC
148          6.5         3.0          5.2         2.0      CC
149          6.2         3.4          5.4         2.3      CC
150          5.9         3.0          5.1         1.8      CC
rezgar
  • 114
  • 8