Reputation: 43
I have a data frame with 2 columns, but I have errors only in the first column and I need to read them by scrolling only the rows of the first column.
player <- c( "ronaldo", "neymar", "zidane", "error", "morata", "suarez")
code <- c("ron897", "ney548", "zid176", "mor458", "sua932", "rob346" )
df <- cbind(player, code)
player code
[1,] "ronaldo" "ron897"
[2,] "neymar" "ney548"
[3,] "zidane" "zid176"
[4,] "error" "mor458"
[5,] "morata" "sua932"
[6,] "suarez" "rob346"
I would like to achieve this. Maybe I need a function that once found "error" deletes it and makes the rows of the columns scale one line up
player code
[1,] "ronaldo" "ron897"
[2,] "neymar" "ney548"
[3,] "zidane" "zid176"
[4,] "morata" "mor458"
[5,] "suarez" "sua932"
[6,] "rob carlos" "rob346"
Upvotes: 0
Views: 86
Reputation: 2748
In effect, what you're trying to do is to delete entries in only one column. This is very possible, but you need to consider how to ensure that both columns stay the same length. The easiest way is to pad the player
column after deleting values. If you pad it with "error"
values you can think of this as a sorting operation:
df$player2 <- c(player[player != "error"], player[player == "error"])
df
#> player code player2
#> 1 ronaldo ron897 ronaldo
#> 2 neymar ney548 neymar
#> 3 zidane zid176 zidane
#> 4 error mor458 morata
#> 5 morata sua932 suarez
#> 6 suarez rob346 error
Upvotes: 2