Home > Software engineering >  R new column in a dataframe that contains a vector generated using conditions from other columns
R new column in a dataframe that contains a vector generated using conditions from other columns

Time:07-05

I have this dataframe df:

df<-structure(list(hex = 1:6, tile_type_index = c(9L, 10L, 5L, 9L, 
3L, 2L)), class = "data.frame", row.names = c(NA, -6L))

   hex tile_type_index
1    1               9
2    2              10
3    3               5
4    4               9
5    5               3
6    6               2

I would like to add a new column called material that looks like the following:

   hex tile_type_index    material
1    1               9    0, 0, 0, 0, 0, 0, 0, 0, 1000, 0
2    2              10    0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3    3               5    0, 0, 0, 0, 1000, 0, 0, 0, 0, 0
4    4               9    0, 0, 0, 0, 0, 0, 0, 0, 1000, 0
5    5               3    0, 0, 1000, 0, 0, 0, 0, 0, 0, 0
6    6               2    0, 1000, 0, 0, 0, 0, 0, 0, 0, 0

The rules of the column material are that it is a vector of length 10 where the element in the vector with index given in tile_type_index is 1000 and the rest are 0's. This is true unless tile_type_index==10 in which case all elements in the vector are 0.

How can I generate this column?

CodePudding user response:

One possible way to solve your problem:

# way 1
df$material = lapply(df$tile_type_index, \(x) match(1:10, x, 0, 10) * 1000)
# way 2 
df$material = lapply(df$tile_type_index, \(x) (x!=10 & x==1:10) * 1000)

#   hex tile_type_index                        material
# 1   1               9 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0
# 2   2              10    0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 3   3               5 0, 0, 0, 0, 1000, 0, 0, 0, 0, 0
# 4   4               9 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0
# 5   5               3 0, 0, 1000, 0, 0, 0, 0, 0, 0, 0
# 6   6               2 0, 1000, 0, 0, 0, 0, 0, 0, 0, 0

CodePudding user response:

You may try

library(dplyr)
df %>%
  rowwise %>%
  mutate(material = list(1000 * replace_na(match(1:10, ifelse(tile_type_index == 10, 0, tile_type_index)), 0))
        %>% as.character() # to see it's value
        ) 

    
    hex tile_type_index material                          
  <int>           <int> <chr>                             
1     1               9 c(0, 0, 0, 0, 0, 0, 0, 0, 1000, 0)
2     2              10 c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)   
3     3               5 c(0, 0, 0, 0, 1000, 0, 0, 0, 0, 0)
4     4               9 c(0, 0, 0, 0, 0, 0, 0, 0, 1000, 0)
5     5               3 c(0, 0, 1000, 0, 0, 0, 0, 0, 0, 0)
6     6               2 c(0, 1000, 0, 0, 0, 0, 0, 0, 0, 0)

CodePudding user response:

df<-structure(list(hex = 1:6, tile_type_index = c(9L, 10L, 5L, 9L, 3L, 2L), material = matrix(0,6,10)), class = "data.frame", row.names = c(NA, -6L))
for(i in 1:6){
if(df$tile_type_index[i] != 10){
    df$material[i,df$tile_type_inde[i]] <- 1000
   }
}
df
hex tile_type_index material
<int>   <int>   <dbl[,10]>
1   9   0, 0, 0, 0, 0, 0, 0, 0, 1000, 0
2   10  0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3   5   0, 0, 0, 0, 1000, 0, 0, 0, 0, 0
4   9   0, 0, 0, 0, 0, 0, 0, 0, 1000, 0
5   3   0, 0, 1000, 0, 0, 0, 0, 0, 0, 0
6   2   0, 1000, 0, 0, 0, 0, 0, 0, 0, 0

how about this

  •  Tags:  
  • r
  • Related