简体   繁体   中英

R remove characters in even rows of a specific column

I'm trying to remove the characters in even rows of a specific column.

My dataset looks like this:

name value
apple 3
apple (0,1)
banana 6
banana (-2,6)
cherry 3
cherry (4,6)

And this is what I'm expecting:

name value
apple 3
(0,1)
banana 6
(-2,6)
cherry 3
(4,6)

Thank you!

You may try:

df$name[!c(TRUE, FALSE)] <- ""

This will assign empty string to even rows of the name column.

If the goal is to remove all the name values which are repeated, you may use duplicated .

df$name[duplicated(df$name)] <- ''
df

#    name  value
#1  apple      3
#2         (0,1)
#3 banana      6
#4        (-2,6)
#5 cherry      3
#6         (4,6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM