简体   繁体   中英

compare two columns in r with a for loop?

I have a data.frame in R with two columns, we'll call them X1 and X2. I want to write a for loop that compares if X1 < X2 and then create a new column that is the smaller number of the two columns. I'm familiar with for loops in javascript but R seems to make it difficult. any help would be greatly appreciated, Thanks!

   X1   X2
   -3.0 42.0
   42.0  0.0
   -7.0 43.0
   -7.0 47.5
   -9.0 45.5
   -5.5 49.5
   -8.5 45.5
   -3.0 43.5
    0.0 -3.0
   49.5 -9.0
   43.5 -4.5
   -6.5 43.5
   -3.0 45.5

output would look something like this ...

  X1   X2  X3
   -3.0 42.0 -3.0
   42.0  0.0  0.0
   -7.0 43.0 -7.0
   -7.0 47.5 -7.0
   -9.0 45.5 ... and so on
   -5.5 49.5
   -8.5 45.5
   -3.0 43.5
    0.0 -3.0
   49.5 -9.0
   43.5 -4.5
   -6.5 43.5
   -3.0 45.5

The built-in way to do it:

your_data$X3 = pmin(your_data$X1, your_data$X2)

R is about vectorization rather than for loops.

If you didn't know about (and couldn't find) pmin , this would be another vectorized way to do it:

your_data$X3 = ifelse(your_data$X1 < your_data$X2, X1, X2)

If you really want a for loop with min , it would go something like this:

your_data$X3 = NA # initialize the column
for (i in 1:nrow(your_data)) {
    your_data$X3[i] = min(your_data$X1[i], your_data$X2[i])
}

But note that this is trying to write R code like it's some other language. It will be slower, it obviously takes more typing, and to someone used to reading R code it's less clear.

Another option is:

df$X3 <- apply(df, 1, min)

Output :

     X1   X2   X3
1  -3.0 42.0 -3.0
2  42.0  0.0  0.0
3  -7.0 43.0 -7.0
4  -7.0 47.5 -7.0
5  -9.0 45.5 -9.0
6  -5.5 49.5 -5.5
7  -8.5 45.5 -8.5
8  -3.0 43.5 -3.0
9   0.0 -3.0 -3.0
10 49.5 -9.0 -9.0
11 43.5 -4.5 -4.5
12 -6.5 43.5 -6.5
13 -3.0 45.5 -3.0

Data:

df <- sstructure(list(X1 = c(-3, 42, -7, -7, -9, -5.5, -8.5, -3, 0, 
      49.5, 43.5, -6.5, -3), X2 = c(42, 0, 43, 47.5, 45.5, 49.5, 45.5, 
      43.5, -3, -9, -4.5, 43.5, 45.5)), .Names = c("X1", "X2"), class = "data.frame", row.names = c(NA, 
     -13L))

您可以使用ifelse语句,

df[,3] <- ifelse(df[,1] < df[,2], df[,1],df[,2])

Here's how I managed to use a for loop to compare two columns in a .csv file called 'data':

for (i in 1:nrow(data)) { 
    print(c(i, ifelse((as.character(data[i,1]) == as.character(data[i,2])),"match","mismatch"))) 
        { next } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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