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

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

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:

更多研究:

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

r语言中paste函数

  • 14
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值