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

左手的ㄟ右手 2023-02-28 06:31 234阅读 0赞

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()函数的语法是,

  1. 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()函数的语法是,

  1. 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’参数更改分隔符值。

  1. 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()函数作为输入。

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

Output = “1_two_3_four_5_six”

输出=“ 1_two_3_four_5_six”

  1. 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.

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

  1. 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”

  1. 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参数将使用特定值将元素连接为单个字符串。

  1. 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

  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()函数如何工作。

  1. 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参数是字符,符号或用于分隔元素的值。

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

Output = “df1_df2_df3_df4_df5”

输出=“ df1_df2_df3_df4_df5”

  1. 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”数据集一起工作。

Bod

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

Output = “1_2_3_4_5_7”

输出=“ 1_2_3_4_5_7”

  1. datasets::BOD
  2. 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 documentation

    R文档

  • https://www.youtube.com/watch?v=_mNnbWGAroU

    https://www.youtube.com/watch?v=_mNnbWGAroU

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

r语言中paste函数

发表评论

表情:
评论列表 (有 0 条评论,234人围观)

还没有评论,来说两句吧...

相关阅读

    相关 paste

    paste单词意思是粘贴。该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反。 粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同。paste将

    相关 RSample函数

    今天介绍一些运算函数,它们的使用很简单,没有什么难度,但是也会用的着。 在医学统计学或者流行病学里的现场调查、样本选择经常会提到一个词:随机抽样。随机抽样是为了保证各比较组之