Need to shift some data values in columns to the right but others the same

I have a datable that needs to have some columns shift while the others need to stay in the same column. Is there a way to do this? I am also looking for an if statement where this will only happen if certain columns contain NULL (NA) values.

I have attached an excel of what I am trying to accomplish.

Please let me know if you need more info

That is very challenging to do in R, as R works on columns: if the entries are in the wrong column that means something went very wrong in a step before!

How did you get this data? Is there any way that the original file has the data in the right columns, and it's the importing in Excel that messed it up?

If you really have no other choices, you can always "shift" columns by hardcoding everything:

row <- 1
data[row, "gender"] <- data[row, "car"]
data[row, "car"] <- data[row, "hometown"]
data[row, "hometown"] <- data[row, "birthday"]
[...]

I am importing a pdf and one of the column cells is blank. That is why it is impossible to fix the data beforehand. :frowning: I have a workaround that works now using an if else statement, but wondering if there is a more permanent solution.

Why is the work around not the solution ?

Additionally, I have a quibble with the way you posed your challenge;
If someone wished to use example data to test code against, they would type it out from your screenshot...

This is very unlikely to happen, and so it reduces the likelihood you will receive the help you desire.
Therefore please see this guide on how to reprex data. Key to this is use of either datapasta, or dput() to share your data as code

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.