gpt4 book ai didi

r - 如何拆分但忽略R中引用字符串中的分隔符?

转载 作者:行者123 更新时间:2023-12-01 08:52:56 25 4
gpt4 key购买 nike

我正在拆分用逗号分隔的字符串,但是,我想忽略引号之间的逗号。这是一个例子:

library(data.table)
dataset <- data.frame(str=c("USATW,\"USA Technologies, Inc Warrants\",Q" ,
"DUSA,DUSA Pharmaceuticals Inc,Q"))

#1 USATW,"USA Technologies, Inc Warrants",Q
#2 DUSA,DUSA Pharmaceuticals Inc,Q

setDT(dataset)[, c("Symbol","Security Name","Market Category") :=
tstrsplit(str, ",", fixed=TRUE)]


# Symbol Security Name Market Category
#1 USATW "USA Technologies Inc Warrants"
#2 DUSA DUSA Pharmaceuticals Inc Q

第一个字符串应该是:

#1  USATW    "USA Technologies, Inc Warrants"  Q

有类似的帖子,但使用其他编程语言。

最佳答案

试试 read.table。不需要任何软件包。

read.table(text = as.character(dataset$str), sep = ",", as.is = TRUE,   
col.names = c("Symbol", "Security Name", "Market Category"), check.names = FALSE)

给予:

  Symbol                  Security Name Market Category
1 USATW USA Technologies, Inc Warrants Q
2 DUSA DUSA Pharmaceuticals Inc Q

关于r - 如何拆分但忽略R中引用字符串中的分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36771411/

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