I have an R data frame that looks like this :
I would like to insert the same string into specific rows and into one column, like this (image 2):
e.g. String 'zoo' into a single column 'groups' in only rows 1 ,3 and 6
I have an R data frame that looks like this :
I would like to insert the same string into specific rows and into one column, like this (image 2):
e.g. String 'zoo' into a single column 'groups' in only rows 1 ,3 and 6
You may try:
df[df$RowNumber %in% c(1,3,6), "groups"] <- "zoo"
df
RowNumber groups
1 1 zoo
2 2 <NA>
3 3 zoo
4 4 <NA>
5 5 <NA>
6 6 zoo
7 7 <NA>
Data:
df <- data.frame(RowNumber=c(1:7), groups=rep(NA,7))
Re-using @Tim Biegeleisen's data, you could use the ifelse
function to define the condition under which the values in column groups
should change to zoo
:
df$groups <- ifelse(df$RowNumber==1|df$RowNumber==3|df$RowNumber==6, "zoo", df$groups); df
RowNumber groups
1 1 zoo
2 2 <NA>
3 3 zoo
4 4 <NA>
5 5 <NA>
6 6 zoo
7 7 <NA>