替换逗号转换为数字并引入NA,如果r中有字符输入
我有一个数据框架DF,其中很少有逗号和字符值。我想删除逗号并用空白替换字符值。但想保留Col5。我应该仅将Col1转换为COL4为数字并删除逗号。
col1=c(1,45,30,35)
col2=c(33,"33,345",87,89)
col3=c("67,345",77,90,"reply")
col4=c("silver",43,23,55)
col5=c("gb","rt","dc","ty")
DF=data.frame(col1,col2,col3,col4,col5)
col6=c(1,45,30,35)
col7=c(33,33345,87,89)
col8=c(67345,77,90,"")
col9=c("",43,23,55)
col10=c("gb","rt","dc","ty")
DF_Output=data.frame(col6,col7,col8,col9,col10)
我的预期输出就像df_output。请帮助我解决这个问题。
I have a data frame DF in which few columns are having comma and character values in it. I want to remove comma and replace character values with blank. But want to retain col5 as it is. I should convert only col1 to col4 to numeric and remove comma.
col1=c(1,45,30,35)
col2=c(33,"33,345",87,89)
col3=c("67,345",77,90,"reply")
col4=c("silver",43,23,55)
col5=c("gb","rt","dc","ty")
DF=data.frame(col1,col2,col3,col4,col5)
col6=c(1,45,30,35)
col7=c(33,33345,87,89)
col8=c(67345,77,90,"")
col9=c("",43,23,55)
col10=c("gb","rt","dc","ty")
DF_Output=data.frame(col6,col7,col8,col9,col10)
My expected output is like DF_Output. Please help me to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试
You may try
您可以从
read> readr
软件包中使用parse_number
:You can use
parse_number
fromreadr
package as well: