split.cols | R Documentation |
This function outputs an inputted matrix/dataframe/table with splitted given columns provided a token for split and new column names.
split.cols(x,split.col=NULL,split.char=NULL,split.names=NULL,list.name="variable")
x |
A (list of) matrix/dataframe/table with columns to split. |
split.col |
Numeric. The column number(s) to split. Row names can also be splitted if |
split.char |
Character. The token to split values of the given column(s). If several columns are provided, there can either be a single |
split.names |
Character. The name of the final splitted columns. For multiple columns to split, there can be either only two names that are re-used for all splitted columns, or twice new column names as columns to split with a pair for each, or no provided names and the final output will be 'oldcolumnname'-1 and 'oldcolumnname'-2 |
list.name |
Character. If |
# Create a matrix with five character vectors, two of each (first and third) having each a token between values (a '-' and a '+')
set.seed(1)
vec1<-paste(sample(letters[7:9],20,TRUE),toupper(sample(letters[7:9],20,TRUE)),sep="-")
vec2<-sample(letters[1:5],20,TRUE)
vec3<-paste(sample(letters[1:10],20,TRUE),rep("way",20),sep="+")
vec4<-toupper(sample(letters[22:26],20,TRUE))
vec5<-sample(c("one","two","three"),20,TRUE)
x<-cbind(vec1,vec2,vec3,vec4,vec5)
x
# Split the first and third columns with their tokens, with default final column names
split.cols(x,c(1,3),c("-","\\+"))
# Do the same with a bit of variation for three lists
x<-list()
for(i in 1:3){
set.seed(i)
vec1<-paste(sample(letters[(7:9)+i],20,TRUE),toupper(sample(letters[(7:9)+i],20,TRUE)),sep="-")
vec2<-sample(letters[(1:5)*i],20,TRUE)
vec3<-paste(sample(letters[(1:10)+i],20,TRUE),rep(c("way","path","road")[i],20),sep="+")
vec4<-toupper(sample(letters[(22:26)-(3*i)],20,TRUE))
vec5<-sample(c("one","two","three","four","five")[i:(i+2)],20,TRUE)
x<-c(x,list(cbind(vec1,vec2,vec3,vec4,vec5)))
}
names(x)<-c("firstest","secondtest","thirdtest")
# Now split columns 1 and 3 of each element of x and name it "test row"
split.cols(x,c(1,3),c("-","\\+"),list.name="test row")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.