?s 21:05 de 11/06/2023, javad bayat escreveu:> Dear R users; > I am trying to fill a column based on a specific value in another column of > a dataframe, but it seems there is a problem with the codes! > The "Layer" and the "LU" are two different columns of the dataframe. > How can I fix this? > Sincerely > > > for (i in 1:nrow(data2$Layer)){ > if (data2$Layer == "Level 12") { > data2$LU == "Park" > } > } > > > >Hello, There are two bugs in your code, 1) the index i is not used in the loop 2) the assignment operator is `<-`, not `==` Here is the loop corrected. for (i in 1:nrow(data2$Layer)){ if (data2$Layer[i] == "Level 12") { data2$LU[i] <- "Park" } } But R is a vectorized language, the following two ways are the idiomac ways of doing what you want to do. i <- data2$Layer == "Level 12" data2$LU[i] <- "Park" # equivalent one-liner data2$LU[data2$Layer == "Level 12"] <- "Park" If there are NA's in data2$Layer it's probably safer to use ?which() in the logical index, to have a numeric one. i <- which(data2$Layer == "Level 12") data2$LU[i] <- "Park" # equivalent one-liner data2$LU[which(data2$Layer == "Level 12")] <- "Park" Hope this helps, Rui Barradas
@vi@e@gross m@iii@g oii gm@ii@com
2023-Jun-11 14:58 UTC
[R] Problem with filling dataframe's column
The problem being discussed is really a common operation that R handles quite easily in many ways. The code shown has way too many things that do not fit to make much sense and is not written the way many R programmers would write it. Loops like the one used are legal but not needed. As has been noted, use of "==" for assignment is the wrong choice. Not using some method to refer to a specific cell would still result in odd behavior. One accepted and common method s to do this vectorized as you are dealing with two vectors of the same size. Code like: Matches <- Data2$Layer == "Level 12" Will result in a Boolean vector containing TRUE where it found a match, FALSE otherwise. Now you can use the above as a sort of indexing as in: data2$LU[Matches] <- "Park" Only the indexes marked TRUE will be selected and set to the new value. Of course, the two lines can be combined as: data2$LU[Data2$Layer == "Level 12"] <- "Park" There are also alternatives where people use functions like ifelse() also in base R. And, of course, some people like alternate packages such as in the Tidyverse where you might have used a mutate() or other methods. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of javad bayat Sent: Sunday, June 11, 2023 4:05 PM To: R-help at r-project.org Subject: [R] Problem with filling dataframe's column Dear R users; I am trying to fill a column based on a specific value in another column of a dataframe, but it seems there is a problem with the codes! The "Layer" and the "LU" are two different columns of the dataframe. How can I fix this? Sincerely for (i in 1:nrow(data2$Layer)){ if (data2$Layer == "Level 12") { data2$LU == "Park" } } -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat194 at yahoo.com [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Dear R users; I am trying to fill a column based on a specific value in another column of a dataframe, but it seems there is a problem with the codes! The "Layer" and the "LU" are two different columns of the dataframe. How can I fix this? Sincerely for (i in 1:nrow(data2$Layer)){ if (data2$Layer == "Level 12") { data2$LU == "Park" } } -- Best Regards Javad Bayat M.Sc. Environment Engineering Alternative Mail: bayat194 at yahoo.com [[alternative HTML version deleted]]