博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
r语言中paste函数_R中的paste()函数-简要指南
阅读量:2530 次
发布时间:2019-05-11

本文共 5717 字,大约阅读时间需要 19 分钟。

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 .

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

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

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

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

Output = “1_2_3_4_5_7”

输出=“ 1_2_3_4_5_7”

datasets::BODpaste(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 . Happy pasting!!!

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

More study:

更多研究:

翻译自:

r语言中paste函数

转载地址:http://smozd.baihongyu.com/

你可能感兴趣的文章
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>