2017-06-21 39 views
-2
# 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来添加“苹果”和“橙子”。 请帮我解决这个问题。R编程数据类型转换

+0

您是否在寻找'帮助(“C “)'?请阅读R的介绍。 – Roland

+4

“my_fruit”的期望结果是什么? – simone

回答

1

您无法为添加添加数值和文本值。如果两个变量都是数字/整数,则可以将这些值相加。如果两个变量是字符串,可以使用膏状()

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) 

你能看到什么?粘贴功能加入他们,并期待在崩溃和SEP参数

+0

只能对数值进行添加。已经通知。粘贴功能可以帮助您进行连接。 x =“我爱”y =“stackoverflow”z = paste(x,y)将一起打印这些语句。 – Nirmal

+0

感谢您的答案兄弟。我们可以使用as.characer()来做一些相关的计算 –

+0

as.character会将字符串转换为字符。请看链接学习R https://www.analyticsvidhya.com/blog/2016/02/complete-tutorial-learn-data-science-scratch/ – Nirmal

0
x<-c(1:100) 
x[41:50] will give you the output. everything is a list in r. 
+0

没有兄弟的数量范围输出,我需要得到从4开始的数字输出。 –