仅将列的一部分转换为数字

时间:2018-06-28 20:44:03

标签: r for-loop

我的数据框看起来像

df <- data.frame("info" = c("food", "number", "price"), 
"a" = c("Melon","1.84+", NA), 
"b" = c("Berry" ,"4.49+", "44"), 
"c" = c("Orange", NA, "4.40"), 
"d" = c("Apple","9", "1.94+"))`

我想要的是从数字中删除所有加号,然后将其更改为数字。

我尝试过

for (i in 1:nrow(df)){
     for (j in 1:ncol(df)){
          if (i > 1 & j > 1){
              df[[i,j]] <<- as.numeric(as.character(gsub("\\+", "", df[[i,j]])))
}}}

运行上述代码后,加号已成功删除,但是当我检查数字单元格的类时,R会返回"character"

只是想知道是否有人对此有任何想法? 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

在我看来,您的数据定向是错误的。您可以以更有意义的格式隐藏data.frame,然后将列更改为数字。使用gatherspread可以重新排列数据。

使用tidyverse的一个选项可以是:

library(tidyverse)


df %>% gather(key, value, - info) %>%
  spread(info, value) %>%
  mutate_at(vars(number:price), funs(as.numeric(gsub("\\+", "",.))))

#Result -- Its same data but represented in slightly different way.

#   key   food number price
# 1   a  Melon   1.84    NA
# 2   b  Berry   4.49 44.00
# 3   c Orange     NA  4.40
# 4   d  Apple   9.00  1.94

数据:

df <- data.frame("info" = c("food", "number", "price"), 
                 "a" = c("Melon","1.84+", NA), 
                 "b" = c("Berry" ,"4.49+", "44"), 
                 "c" = c("Orange", NA, "4.40"), 
                 "d" = c("Apple","9", "1.94+"))

答案 1 :(得分:0)

在基R中,您可以执行以下操作:

 cbind(nn=names(df[-1]),read.table(text=gsub("\\+","",do.call(paste,data.frame(t(df)))),h=T))
  nn   food number price
1  a  Melon   1.84    NA
2  b  Berry   4.49 44.00
3  c Orange     NA  4.40
4  d  Apple   9.00  1.94