Search code examples
rrowassign

Assign value to a specific rows (R)


I have a df of 16k+ items. I want to assign values A, B and C to those items based.

Example: I have the following df with 10 unique items

df <- c(1:10)

Now I have three separate vectors (A, B, C) that contain row numbers of the df with values A, B or C.

A <- c(3, 9)
B <- c(2, 6, 8)
C <- c(1, 4, 5, 7, 10)

Now I want to add a new category column to the df and assign values A, B and C based on the row numbers that are in the three vectors that I have. For example, I would like to assign value C to rows 1, 4, 5, 7 and 10 of the df.

I tried to experiment with for loops and if statements to match the value of the vector with the row number of the df but I didn't succeed. Can anybody help out?


Solution

  • Here is a way to assign the new column.

    Create the data frame and a list of vectors:

    df <- data.frame(n=1:10)
    dat <- list( A=c(3, 9), B=c(2, 6, 8), C=c(1, 4, 5, 7, 10) )
    

    Put the data in the desired rows:

    df$new[unlist(dat)] <- sub("[0-9].*$","",names(unlist(dat)))
    

    Result:

    df
        n new
    1   1   C
    2   2   B
    3   3   A
    4   4   C
    5   5   C
    6   6   B
    7   7   C
    8   8   B
    9   9   A
    10 10   C