首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代一列的每行中的每个字符

迭代一列的每行中的每个字符
EN

Stack Overflow用户
提问于 2018-11-09 03:29:13
回答 1查看 29关注 0票数 0

该列的示例为test <- c('apple #1930', 'apple #84555', 'apple A #33859', 'apple good', 'peach brand A - level 1 #8839', 'peach brand A - middle or not', 'peach brand A #2283')

我希望我的结果表是这样的:

代码语言:javascript
复制
 Name           Description     Number
apple              NA           #1930
apple              NA           #84555
apple              A            #33859
apple             good            NA
peach brand A     level 1        #8839
peach brand A    middle or not      NA
peach brand A       NA           #2283

我试过了`

代码语言:javascript
复制
findiffs <- rle(test)

newdf <- data.frame(
                    firststring = test[cumsum(findiffs$length)],
                    secondstring = test[cumsum(findiffs$length)+1]
                    )

newdf <- newdf[-dim(newdf)[1],] 

但是它没有给我想要的输出。

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2018-11-09 03:40:35

我猜每一列都有它自己的分隔符。因此,您可能想要尝试这样的操作:

代码语言:javascript
复制
test <- data.frame(orig = c('apple #1930', 'apple #84555', 'apple A #33859', 'apple good', 'peach brand A - level 1 #8839', 'peach brand A - middle or not', 'peach brand A #2283'))


test %>% separate(orig, into= c("a", "b"), sep = "[#]") %>%  separate(a, into=c("aa", "bb"), sep="[-]")


              aa             bb     b
1         apple            <NA>  1930
2         apple            <NA> 84555
3       apple A            <NA> 33859
4     apple good           <NA>  <NA>
5 peach brand A        level 1   8839
6 peach brand A   middle or not  <NA>
7 peach brand A            <NA>  2283
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53214847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档