r语言中paste函数

Using the paste() function in R will be straight and simple. In this tutorial let’s see how we can use paste() to concatenate the strings and values.

在R中使用paste()函数将很简单。 在本教程中,让我们看看如何使用paste()连接字符串和值。

paste(): Takes multiple elements from the multiple vectors and concatenates them into a single element.

paste():从多个向量中获取多个元素,并将它们连接为一个元素。

Along with paste() function, R has another function named paste0(). Yes, you heard it right.

R与paste()函数一起,还有另一个名为paste0()的函数。 是的,您没听错。

paste0(): The paste0() function has space as its default separator and limits your opportunities in the output as well.

paste0(): paste0()函数使用空格作为其默认分隔符,并且还会限制您在输出中的机会。



让我们从语法开始 (Let’s start with the syntax)

The syntax of the paste() function is,

paste()函数的语法是,


paste(x,sep=" ", collapse=NULL)

Here:

这里:

  • x = vector having values.x =具有值的向量。
  • sep = separator symbols that can be used to separate the elements.sep =可用于分隔元素的分隔符。
  • collapse = It gives a value to collapse.崩溃=它给出一个值来崩溃。

The syntax of the paste0() function is,

paste0()函数的语法是,


paste(x,collapse=NULL)

Where,

哪里,

  • x = vector having the values.x =具有值的向量。
  • collapse = It gives a value to collapse.崩溃=它给出一个值来崩溃。


如何在R中使用paste()函数? (How to use the paste() function in R?)

A simple paste() will take multiple elements as inputs and concatenate those inputs into a single string. The elements will be separated by a space as the default option. But you can also change the separator value using the ‘sep’ parameter.

一个简单的paste()将多个元素作为输入,并将这些输入连接到一个字符串中。 元素将以空格分隔作为默认选项。 但是您也可以使用'sep'参数更改分隔符值。


paste(1,'two',3,'four',5,'six')

Output = “1 two 3 four 5 six”

输出=“ 1 2 3 4 5 6”



使用paste()和分隔符参数 (Using paste() with a separator argument)

The separator parameter in the paste() function will deal with the value or the symbols which are used to separate the elements, which is taken as input by the paste() function.

paste()函数中的spacer参数将处理用于分隔元素的值或符号,这些值或符号由paste()函数作为输入。


paste(1,'two',3,'four',5,'six',sep = "_")

Output = “1_two_3_four_5_six”

输出=“ 1_two_3_four_5_six”


paste(1,'two',3,'four',5,'six',sep = "&")

Output = “1&two&3&four&5&six”

输出=“ 1&two&3&four&5&six”



具有折叠参数的paste()函数 (The paste() function with collapse argument)

When you pass a paste argument to a vector, the separator parameter will not work. Hence here comes the collapse parameter, which is highly useful when you are dealing with the vectors. It represents the symbol or values which separate the elements in the vector.

将粘贴参数传递给矢量时,分隔符参数将不起作用。 因此,出现了坍塌参数,当您处理向量时,该参数非常有用。 它表示将向量中的元素分开的符号或值。


paste(c(1,2,3,4,5,6,7,8),collapse = "_")

Output = “1_2_3_4_5_6_7_8”

输出=“ 1_2_3_4_5_6_7_8”


paste(c('Rita','Sam','John','Jat','Cook','Reaper'),collapse = ' and ')

Output = “Rita and Sam and John and Jat and Cook and Reaper”

输出=“ Rita和Sam和John和Jat和Cook和Reaper”



具有分隔符和折叠参数的paste()函数 (The paste() function with both separator and collapse arguments)

Let’s see how separator and collapse arguments will work. The separator will deal with the values which are to be placed in between the set of elements and the collapse argument will make use of specific value to concatenate the elements into single -string.

让我们看看分隔符和折叠参数如何工作。 分隔符将处理将要放置在元素集之间的值,而collapse参数将使用特定值将元素连接为单个字符串。


paste(c('a','b'),1:10,sep = '_',collapse = ' and ')

Output = “a_1 and b_2 and a_3 and b_4 and a_5 and b_6 and a_7 and b_8 and a_9 and b_1

输出=“ a_1和b_2以及a_3和b_4以及a_5和b_6以及a_7和b_8以及a_9和b_1


paste(c('John','Ray'),1:5,sep = '=',collapse = ' and ')

Output = “John=1 and Ray=2 and John=3 and Ray=4 and John=5”

输出=“ John = 1且Ray = 2且John = 3且Ray = 4且John = 5”



如何在R中使用paste0()函数 (How to use paste0() function in R)

Paste0() function acts just like paste function but with a default separator.

Paste0()函数的作用类似于粘贴函数,但具有默认的分隔符。

Let’s see how paste0() function works.

让我们看看paste0()函数如何工作。


paste0('df',1:6)

Output = “df1” “df2” “df3” “df4” “df5” “df6”

输出=“ df1”“ df2”“ df3”“ df4”“ df5”“ df6”

You can see that the paste0() function has the default separator value. Now let’s see how paste0() function works with the collapse parameter.

您可以看到paste0()函数具有默认的分隔符值。 现在,让我们看看paste0()函数如何使用塌陷参数。



使用带有折叠参数的paste0()函数 (Using the paste0() function with collapse argument )

The collapse argument in the paste0() function is the character, symbol, or a value used to separate the elements.

paste0()函数中的collapse参数是字符,符号或用于分隔元素的值。


paste0('df',1:5,collapse = '_')

Output = “df1_df2_df3_df4_df5”

输出=“ df1_df2_df3_df4_df5”


paste0('df',1:5,collapse = ' > ')

Output = “df1 > df2 > df3 > df4 > df5”

输出=“ df1> df2> df3> df4> df5”

As you may observe the above results, the paste0() function returns a string with a default separator and a specified collapse argument as well.

您可能会看到上面的结果,paste0()函数返回一个带有默认分隔符和指定折叠参数的字符串。



如何在R中的数据框中使用paste()函数 (How to use paste() function in a data frame in R)

You can also use the paste() function to paste the values or elements present in a data frame.

您也可以使用paste()函数粘贴数据框中存在的值或元素。

Let’s see how it works with the ‘BOD’ data set.

让我们看看它如何与“ BOD”数据集一起工作。


datasets::BOD
paste(BOD$Time,sep = ',',collapse = '_')

Output = “1_2_3_4_5_7”

输出=“ 1_2_3_4_5_7”


datasets::BOD
paste(BOD$demand,sep = ',',collapse = '_')

Output = “8.3_10.3_19_16_15.6_19.8”

输出=“ 8.3_10.3_19_16_15.6_19.8”



结论 (Conclusion)

R offers numerous functions to make your analysis simpler but efficient. Among them the paste() function is very useful in concatenating the strings and the elements into a single string as well.

R提供了许多功能,可以使您的分析更加简单而有效。 其中的paste()函数在将字符串和元素连接成单个字符串时也非常有用。

In this tutorial we have gone through various aspects of the paste() and paste0() functions. Both these will be really helpful in data analysis.

在本教程中,我们介绍了paste()和paste0()函数的各个方面。 这些都将对数据分析非常有帮助。

That’s all for now. Stay tuned for more R tutorials. Happy pasting!!!

目前为止就这样了。 请继续关注更多R教程 。 粘贴愉快!

More study:

更多研究:

  • R documentationR文档
  • https://www.youtube.com/watch?v=_mNnbWGAroUhttps://www.youtube.com/watch?v=_mNnbWGAroU

翻译自: https://www.journaldev.com/40396/paste-in-r

r语言中paste函数

r语言中paste函数_R中的paste()函数-简要指南相关推荐

  1. c语言中math的库函数,C语言中math.h库中的常用函数

    C语言中math.h库中的常用函数 int abs(int i) 返回整型参数i的绝对值 double cabs(struct complex znum) 返回复数znum的绝对值 double fa ...

  2. r语言中mpg数据_R语言数据筛选整理包dplyr

    dplyr软件包是R中功能最强大,最受欢迎的软件包之一.该软件包由最受欢迎的R程序员Hadley Wickham编写,他编写了许多有用的R软件包,如ggplot2,tidyr等.本文包括一些示例和如何 ...

  3. 字符函数和字符串函数_R中的字符串–函数及其操作

    字符函数和字符串函数 Strings are generally a one-dimensional (1D) arrays that contain single or multiple value ...

  4. r语言中mpg数据_R语言常用的数据处理的包(1)

    在R中有很多的内置函数,比如transform().rbind().cbind()等函数,这些函数我们可以直接使用,除此之外,还有常见的几种包在处理数据的时候非常好用. dplyr包 dplyr包是H ...

  5. r语言中mpg数据_R语言数据分析系列之五

    R语言数据分析系列之五 本节来讨论一下R语言的基本图形展示,先来看一张效果图吧. 这是一张用R语言生成的,虚拟的wordcloud云图,详细实现细节请參见我的github项目:https://gith ...

  6. r语言中mpg数据_R语言数据实战 | 统计检验

    原标题:R语言数据实战 | 统计检验 1.单个总体均值的t检验 1. 什么是检验? 检验(test)是统计学中最重要的概念之一,在科学研究和实际业务中都有着广泛的应用.用一句话来概括就是:人们希望通过 ...

  7. r语言中c函数错误,R语言中c()函数与paste()函数的区别说明

    c()函数:将括号中的元素连接起来,并不创建向量 paste()函数:连接括号中的元素 例如 c(1, 2:4),结果为1 2 3 4 paste(1, 2:4),结果为"1 2" ...

  8. r语言中的while循环_R编程中的While循环

    r语言中的while循环 In addition to the for loop we discussed earlier, R also offers another kind of loop to ...

  9. R语言中的apply函数用法

    刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是为什么呢?原因在于R的循环操作for和while,都是基于R语言本身来 ...

最新文章

  1. 快手用旺旺瓶子做机器人_用平底锅做西多士,早餐不发愁,孩子三天两头点名吃,简单快手...
  2. 确认和回调_【短线回调,确认突破点】
  3. 如果重走职场,一定会送自己 8 个锦囊
  4. 《JavaScript高效图形编程(修订版)》——第2章 DHTML基础 2.1创建DHTML sprite
  5. 【已解决】Linux redhat 6.4上安装VNC Server
  6. 语法和c区别_dockerfile语法
  7. sql Server获取表中今天、昨天、本周、上周、本月、上月等数据
  8. 机器面试-处理分类问题
  9. 【渝粤教育】广东开放大学 建筑设备 形成性考核 (33)
  10. 可调稳压电源lm317实验报告_LM317可调直流稳压电源DIY,非常实用!
  11. autocad ios 虚线_autocad 如何画虚线
  12. Python常用音频库
  13. excel软件做折线图
  14. 深度学习三巨头也成了大眼萌,这个一键转换动画电影形象的网站「太火」了...
  15. 微信生成公众号带参数二维码(一)
  16. PHP除数取余数,php相除取余数的实现方法
  17. elementui+vue+springboot企业员工工资考勤请假系统
  18. 「实战」谷歌广告账户可以退款吗?怎么退款?
  19. Android——高德地图地点搜索框
  20. hadoop2.8.2 YARN 架构

热门文章

  1. day08面向对象-内部类、异常
  2. 【java基础】重载与重写
  3. Ext中的get、getDom、getCmp、getBody、getDoc的区别
  4. sql2005数据库备份—sql语句
  5. CF 398 E(动态规划)
  6. centos 设置时间为北京时间
  7. 深度学习最全优化方法总结比较(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)(转)...
  8. iOS 让UIButton根据文字内容自动计算宽高
  9. WEB标准 基础(一) 到底是什么?
  10. 江苏实时分析评价系统项目总结报告