gpt4 book ai didi

r - 加入/合并数据框内的两列

转载 作者:行者123 更新时间:2023-12-04 11:03:55 28 4
gpt4 key购买 nike

说我有这个数据,

(df <- data.frame( col1 = c('My','Your','His','Thir'), col2 = c('Cat','Dog','Fish','Dog')))

col1 col2
1 My Cat
2 Your Dog
3 His Fish
4 Thir Dog

我想组合这样的列
`some magic`

col1 col2 col3
1 My Cat My Cat
2 Your Dog Your Dog
3 His Fish His Fish
4 Thir Dog Thir Dog

我该怎么办?甚至像这样的逗号(,),
`some magic`

col1 col2 col3
1 My Cat My, Cat
2 Your Dog Your, Dog
3 His Fish His, Fish
4 Thir Dog Thir, Dog

最佳答案

df$col3 <- paste(df$col1, df$col2, sep=",") .您也可以使用 sprintfpaste0职能。

df$col3 <- paste(df$col1, df$col2, sep=",") # comma separator
df$col3 <- paste0(df$col1, df$col2) # for no separator

关于r - 加入/合并数据框内的两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13596155/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com