Dear R forum I have a data.frame df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43)) currency_type rates 1 EURO_o_n 0.470 2 EURO_o_n 0.475 3 EURO_1w 0.461 4 EURO_1w 0.464 5 USD_o_n 1.210 6 USD_o_n 1.190 7 USD_1w 1.410 8 USD_1w 1.430 I need to split the values appearing under currency_type to obtain following data.frame in the "original order" currency tenor rates EURO o_n 0.470 EURO o_n 0.475 EURO 1w 0.461 EURO 1w 0.464 USD o_n 1.210 USD o_n 1.190 USD 1w 1.410 USD 1w 1.430 Basically I need to split the currency name and tenors. I tried strsplit(df$currency_type, "_") Error in strsplit(df$currency_type, "_") : non-character argument Kindly guide Katherine [[alternative HTML version deleted]]
On Tue, Apr 16, 2013 at 8:38 AM, Katherine Gobin <katherine_gobin at yahoo.com> wrote:> Dear R forum > > I have a data.frame > > df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43)) > > currency_type rates > 1 EURO_o_n 0.470 > 2 EURO_o_n 0.475 > 3 EURO_1w 0.461 > 4 EURO_1w 0.464 > 5 USD_o_n 1.210 > 6 USD_o_n 1.190 > 7 USD_1w 1.410 > 8 USD_1w 1.430 > > > I need to split the values appearing under currency_type to obtain following data.frame in the "original order" > > currency tenor rates > EURO o_n 0.470 > EURO o_n 0.475 > EURO 1w 0.461 > EURO 1w 0.464 > USD o_n 1.210 > USD o_n 1.190 > USD 1w 1.410 > USD 1w 1.430 > > Basically I need to split the currency name and tenors. >Try sub: with(df, data.frame( currency = sub("_.*", "", currency_type), tenor = sub("^[^_]*_", "", currency_type), rates) ) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hi, Try: df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43),stringsAsFactors=FALSE) ?df$currency<-unlist(lapply(str_split(df[,1],"_"),`[`,1)) ?df$tenor<-unlist(lapply(str_split(df[,1],"_"),function(x) {paste(x[-1],collapse="_")})) ?df[,c(3,4,2)] ?# currency tenor rates #1???? EURO?? o_n 0.470 #2???? EURO?? o_n 0.475 #3???? EURO??? 1w 0.461 #4???? EURO??? 1w 0.464 #5????? USD?? o_n 1.210 #6????? USD?? o_n 1.190 #7????? USD??? 1w 1.410 #8????? USD??? 1w 1.430 A.K. ----- Original Message ----- From: Katherine Gobin <katherine_gobin at yahoo.com> To: r-help at r-project.org Cc: Sent: Tuesday, April 16, 2013 8:38 AM Subject: [R] Splitting the Elements of character vector Dear R forum I have a data.frame df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43)) ? currency_type???? rates 1????? EURO_o_n?? 0.470 2????? EURO_o_n?? 0.475 3?????? EURO_1w?? 0.461 4?????? EURO_1w ? 0.464 5?????? USD_o_n ?? 1.210 6?????? USD_o_n??? 1.190 7??????? USD_1w??? 1.410 8??????? USD_1w??? 1.430 I need to split the values appearing under currency_type to obtain following data.frame in the "original order" currency???? tenor?? rates EURO???????? o_n???? 0.470 EURO???????? o_n???? 0.475 EURO???????? 1w ???? 0.461 EURO???????? 1w ???? 0.464 USD?????????? o_n???? 1.210 USD?????????? o_n???? 1.190 USD ? ? ? ? ? 1w ? ?? 1.410 USD ? ? ? ? ? 1w ? ? ?1.430 ?? Basically I need to split the currency name and tenors. I tried strsplit(df$currency_type, "_") Error in strsplit(df$currency_type, "_") : non-character argument Kindly guide Katherine ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
HI, You can also do this by: library(stringr) df2<-data.frame(currency=word(str_replace(df[,1],"_"," "),1), temor=word(str_replace(df[,1],"_"," "),2), rates=df$rates,stringsAsFactors=FALSE) ?df2 #? currency temor rates #1???? EURO?? o_n 0.470 #2???? EURO?? o_n 0.475 #3???? EURO??? 1w 0.461 #4???? EURO??? 1w 0.464 #5????? USD?? o_n 1.210 #6????? USD?? o_n 1.190 #7????? USD??? 1w 1.410 #8????? USD??? 1w 1.430 A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: Katherine Gobin <katherine_gobin at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Tuesday, April 16, 2013 9:00 AM Subject: Re: [R] Splitting the Elements of character vector Hi, Try: df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43),stringsAsFactors=FALSE) ?df$currency<-unlist(lapply(str_split(df[,1],"_"),`[`,1)) ?df$tenor<-unlist(lapply(str_split(df[,1],"_"),function(x) {paste(x[-1],collapse="_")})) ?df[,c(3,4,2)] ?# currency tenor rates #1???? EURO?? o_n 0.470 #2???? EURO?? o_n 0.475 #3???? EURO??? 1w 0.461 #4???? EURO??? 1w 0.464 #5????? USD?? o_n 1.210 #6????? USD?? o_n 1.190 #7????? USD??? 1w 1.410 #8????? USD??? 1w 1.430 A.K. ----- Original Message ----- From: Katherine Gobin <katherine_gobin at yahoo.com> To: r-help at r-project.org Cc: Sent: Tuesday, April 16, 2013 8:38 AM Subject: [R] Splitting the Elements of character vector Dear R forum I have a data.frame df = data.frame(currency_type = c("EURO_o_n", "EURO_o_n", "EURO_1w", "EURO_1w", "USD_o_n", "USD_o_n", "USD_1w", "USD_1w"), rates = c(0.47, 0.475, 0.461, 0.464, 1.21, 1.19, 1.41, 1.43)) ? currency_type???? rates 1????? EURO_o_n?? 0.470 2????? EURO_o_n?? 0.475 3?????? EURO_1w?? 0.461 4?????? EURO_1w ? 0.464 5?????? USD_o_n ?? 1.210 6?????? USD_o_n??? 1.190 7??????? USD_1w??? 1.410 8??????? USD_1w??? 1.430 I need to split the values appearing under currency_type to obtain following data.frame in the "original order" currency???? tenor?? rates EURO???????? o_n???? 0.470 EURO???????? o_n???? 0.475 EURO???????? 1w ???? 0.461 EURO???????? 1w ???? 0.464 USD?????????? o_n???? 1.210 USD?????????? o_n???? 1.190 USD ? ? ? ? ? 1w ? ?? 1.410 USD ? ? ? ? ? 1w ? ? ?1.430 ?? Basically I need to split the currency name and tenors. I tried strsplit(df$currency_type, "_") Error in strsplit(df$currency_type, "_") : non-character argument Kindly guide Katherine ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.