【问题标题】:R Programming Data Type ConversionR 编程数据类型转换
【发布时间】:2017-11-24 07:55:06
【问题描述】:
 # Assigning a value to the variable my_apples
my_apples <- 5 

# Fixing the assignment of my_oranges
my_oranges <- "six" 

# Creating the variable my_fruit and printing it out
my_fruit <- my_apples + my_oranges 
my_fruit

尝试通过将文本值分配给变量 my_oranges 来添加“apples”和“oranges”。 请帮我解决这个问题。

【问题讨论】:

  • 您在寻找help("c")吗?请阅读 R 简介。
  • my_fruit 的期望结果是什么?

标签: r variables callback type-conversion


【解决方案1】:
x<-c(1:100)
x[41:50] will give you the output. everything is a list in r. 

【讨论】:

  • 不,我需要输出以 4 开头的数字。
【解决方案2】:

您不能将数字值和文本值相加。如果两个变量都是数字/整数,您可以总结这些值。如果两个变量都是字符串,你可以使用 paste() 来添加它们

my_apples <- 5 


# Fixing the assignment of my_oranges
my_oranges <- 6

# Creating the variable my_fruit and printing it out
my_fruit <- my_apples + my_oranges 


my_apples <- "five"


# Fixing the assignment of my_oranges
my_oranges <- "six"

# Creating the variable my_fruit and printing it out
my_fruit <- paste0(my_apples,my_oranges) 

您可以看到 ?paste 函数并查看折叠和 sep 参数

【讨论】:

  • 只能对数值进行加法运算。正如已经告知的那样。粘贴功能可帮助您进行连接。 x = "I love" y = "stackoverflow" z = paste(x,y) 将这些语句一起打印出来。
  • 谢谢兄弟的回答。我们可以在其中使用 as.characer() 来做一些相关的计算吗
  • as.character 会将字符串转换为字符。看链接学习R analyticsvidhya.com/blog/2016/02/…
  • 谢谢兄弟..x
猜你喜欢
  • 1970-01-01
  • 2017-04-10
  • 2015-03-31
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多