1

I am cleaning up some data imported from Excel. I am trying to create a column of values based on the position of a row in a data frame. Specifically, I am trying to assign a value to rows between two rows with specific character values using mutate() and ifelse(). Here is a very simplified example of the data I am working with:

     a        b    
[1,] "5"      "yes"
[2,] "6"      "no" 
[3,] "7"      "no" 
[4,] "2"      "yes"
[5,] "apple"  NA   
[6,] "4"      "yes"
[7,] "1"      "no" 
[8,] "banana" NA   
[9,] "6"      "yes"
[10,] "3"      "yes"

I want to create a c column that returns a character value of colors, where the rows between "apple" and "banana" (row numbers [6] and [7])are assigned a c column value of "red", and all other rows are assigned a value of "blue". Is there a way to do this? Please let me know if I can explain my problem more clearly!

5 Answers 5

2

Using row_number function from dplyr package

#reproducing example
df <- data.frame(a = c("5","6","7","2","apple","4","1","banana","6","3"), b = c("yes","no","no","yes","NA","yes","no","NA","yes","yes"), stringsAsFactors = FALSE)

df$c <- "blue"
lim1 <- which(df$a == "apple")
lim2 <- which(df$a == "banana")

Method 1 :

df$c[lim1:lim2] <- "red"

Method 2 :

library(dplyr)
df <- df %>%
    mutate(c = ifelse(row_number(a) %in% lim1:lim2, "blue", "red"))
2
  • I couldn't get the OP's desired output when I used your method. May I know which version of dplyr are you using? The code mutate(c = ifelse(row_number() %in% (lim1 + 1):(lim2 - 1), "red", "blue")) works for me.
    – HNSKD
    Commented Jul 14, 2017 at 7:54
  • Thanks a lot @HNSKD :), my dplyr version is 0.4.3 though
    – parth
    Commented Jul 14, 2017 at 9:25
1

Firstly your data looks like it's a matrix instead of a data.frame, which you should fix if you plan on using dplyr. Once you get that sorted, you can use cumsum on each term (lagged if you don't want to count apple rows), subtract, and then use ifelse to convert 0 and 1 to blue and red:

library(dplyr)

df <- read.table(text = '  a        b    
[1,] "5"      "yes"
[2,] "6"      "no" 
[3,] "7"      "no" 
[4,] "2"      "yes"
[5,] "apple"  NA   
[6,] "4"      "yes"
[7,] "1"      "no" 
[8,] "banana" NA   
[9,] "6"      "yes"
[10,] "3"      "yes"', header = TRUE, stringsAsFactors = FALSE)

rownames(df) <- NULL

df %>% 
    mutate(c = cumsum(lag(a, default = '') == 'apple') - cumsum(a == 'banana'),
           c = ifelse(as.logical(c), 'red', 'blue'))
#>         a    b    c
#> 1       5  yes blue
#> 2       6   no blue
#> 3       7   no blue
#> 4       2  yes blue
#> 5   apple <NA> blue
#> 6       4  yes  red
#> 7       1   no  red
#> 8  banana <NA> blue
#> 9       6  yes blue
#> 10      3  yes blue
1
  • Nice answer. This also works if there are multiple intervening rows between "apple" and "banana".
    – eipi10
    Commented Jul 15, 2017 at 2:51
1

We can get the positions programmatically and then do the assign

i1 <- Reduce(`:`, which(is.na(df1$b))+ c(1, -1))
df1$c <- 'blue'
df1$c[i1] <- 'red'

data

df1 <- structure(list(a = c("5", "6", "7", "2", "apple", "4", "1", "banana", 
"6", "3"), b = c("yes", "no", "no", "yes", NA, "yes", "no", NA, 
"yes", "yes")), .Names = c("a", "b"), class = "data.frame", row.names = c(NA, 
-10L))
0

The dplyr package offers a row_number() function that can be used in conjunction with mutate and ifelse to assign values to specific row positions:

library(dplyr)
df = df %>% mutate(c=ifelse(row_number(a) %in% c(6,7),"red","blue"))
0

with mutate and dplyr:

df %>% mutate(c = ifelse(row_number() %>% between(match("apple",a)+0.1,match("banana",a)-0.1),"red","blue"))

with base:

df <- transform(df,c = ifelse(1:nrow(df) > match("apple",a) & (1:nrow(df) < match("banana",a) ),"red","blue"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.