I am trying to add a column with repetitive values to an existing dataframe. There are 8 values to be repeated and it should be repeated untill the end of the existing dataframe. I used the code below and got back the Error as below.
I think it has something to due with the fact that the length of the new col1 always comes in multiplies of 8. While the existing dataframe is from a different length.
Any idea how to make the rep function stop at the end of the lentgh of my df? (and thereby making the last rep to stop in the middle of a rep).
Error:
! Assigned data `rep(...)` must be compatible with existing data.
✖ Existing data has 164 rows.
✖ Assigned data has 120 rows.
ℹ Only vectors of size 1 are recycled.
Thanks for the suggestions. Unfortunately they didn't do the trick, see below.
Thank you anyway, always nice to get some help.
Dear @nirgrahamuk,
Your solution did exactly what I wanted, thank you!
> ROW_PCR_OVERVIEW_long_dropNA_wider$RP_row <- rep(c("row_A", "row_B", "row_C", "row_D", "row_E", "row_F", "row_G", "row_H"),
+ dim(ROW_PCR_OVERVIEW_long_dropNA_wider)[2])
Error:
! Assigned data `rep(...)` must be compatible with existing data.
✖ Existing data has 164 rows.
✖ Assigned data has 120 rows.
ℹ Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
> ROW_PCR_OVERVIEW_long_dropNA_wider$RP_row <- rep(c("row_A", "row_B", "row_C", "row_D", "row_E", "row_F", "row_G", "row_H"),
+ nrow(ROW_PCR_OVERVIEW_long_dropNA_wider))
Error:
! Assigned data `rep(...)` must be compatible with existing data.
✖ Existing data has 164 rows.
✖ Assigned data has 1312 rows.
ℹ Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
Technocrat did underline a useful fact about data.frames that many dont realise, the 'length' of a data.frame in R is judged to be its columns, i.e. synonymous with ncol() so nrow() is preferred to find its vertical height.
Thank you @nirgrahamuk, it works very well in my dataframe. I am using it to make a script that can plan our laboratory tests. I do get the more simple data manipulations to work myself now, but some things are just to difficult for me at the moment.
If you don't mind, there is one more bit of code that I need to complete a full section of codes. It is rather similar to the rep-topic here.
I need a column (y) added to my df, in which is repeated 8 times the "1", then 8 times the "2", then 8 times the "3", etc., for as long as the df is.
@nirgrahamuk thanks again for your fast reply. Unfortunately, I am not able to solve this issue with the suggestion you gave me. I'll post it as a new topic.