小编Keo*_*eon的帖子

R中的整数类和数字类有什么区别

我想说这是一个绝对的编程初学者,所以请原谅这个问题是多么基本.

我试图更好地理解R中的"原子"类,也许这适用于编程中的类.我理解字符,逻辑和复杂数据类之间的区别,但我很难找到数字类和整数类之间的根本区别.

假设我有一个简单x <- c(4, 5, 6, 6)的整数向量,这对于整数类是有意义的.但是,当我这样做class(x),我得到[1] "numeric".然后,如果我将此向量转换为整数类x <- as.integer(x).除了类不同之外,它返回相同的精确数字列表.

我的问题是为什么会出现这种情况,以及为什么一组整数的默认类是一个数字类,以及将整数设置为数字而不是整数的优点和缺点是什么.

integer r class numeric object

83
推荐指数
3
解决办法
11万
查看次数

TypeError:无法将Array转换为字符串

我在尝试使用Nokogiri运行rake任务时遇到此错误.

这是代码.

namespace :tops do
  desc "Get Tops Description"
  task get_description: :environment do
  require 'nokogiri'
  require 'open-uri'

 doc = Nokogiri::HTML(open("http://ceratoboutique.com/collections/tops"))

  price = Array.new 
  items = Array.new
  brands = Array.new
  sale_price = Array.new
  image = Array.new
  item_url = Array.new

  price = doc.xpath("//del").collect {|node| node.text.strip}
  items = doc.xpath("//div/a/h4").collect {|node| node.text.strip}
  brands = doc.xpath("//span[contains(@class,'vendor')]").collect {|node| node.text.strip}
  sale_price = doc.xpath("//span[contains(@class, 'price')]/text()").collect {|node| node.text.strip}
  image = doc.xpath("/div/a/img/@src").collect {|node| node.text.strip}
  item_url = doc.css('div.details a').map { |link| link['href'] }.collect 

  price.each do |prices|
      Tops.create(description: price)
    end
  end
end …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails nokogiri rake-task

1
推荐指数
1
解决办法
5518
查看次数

无法从ruby数组中删除NaN

我有一系列的价值观

=> [0.0, 4.76, 0.0, Infinity, NaN, 2.63, 0.74, 10.0, NaN, NaN, NaN, NaN, 0.0, NaN, NaN, NaN, NaN, NaN, Infinity, 5.26, NaN, 0.0, NaN, 3.45, 2.5, NaN, 10.0, 0.0, NaN, 2.94, NaN, NaN, 0.0, 2.04, 0.0, 11.11, NaN, NaN, 1.23, NaN, NaN, 11.11, NaN, NaN, NaN, 0.0, 9.68, NaN, NaN, 10.0, 5.0, 3.7, 10.0, Infinity, 0.0, 0.0, 1.41, NaN, 3.45, NaN]
Run Code Online (Sandbox Code Playgroud)

当我运行这个脚本来删除NaN时,它删除了一些但不是所有的NaN.

  def remove_from_array(numArray)
    numArray.inject(0) do |i|
      if numArray[i].nan?
        numArray.delete_at(i)
      end
      i += 1
    end
   numArray
  end
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

ruby

1
推荐指数
1
解决办法
690
查看次数

标签 统计

ruby ×2

class ×1

integer ×1

nokogiri ×1

numeric ×1

object ×1

r ×1

rake-task ×1

ruby-on-rails ×1