Home > database >  How do I split one-column data separated by spaces into multiple columns?
How do I split one-column data separated by spaces into multiple columns?

Time:02-14

I need to split the column "leg1" into three columns (v1,v2,v3). The problem is that the amount of blank spaces is different by rows.

> head(data[,2:3])
    name                           leg1
513    1      0.00000  0.00000  0.00000
514    2   -0.00000  -0.00000  -0.00000
515    3      0.00000  0.00000  0.00000
516    4   -0.03467  -0.03848  -0.02331
517    5   -0.00000  -0.00000  -0.00000
518    6   -0.00000  -0.00000  -0.00000

I tried to use gsub or substr, but I couldn`t do it.

Could anyone help me, please?

Thanks in advance!!

CodePudding user response:

It may be easier to do this with read.table from base R on the 'leg1' column which will split by the default space

cbind(data[-2], read.table(text = data$leg1, header = FALSE))

-output

  name       V1       V2       V3
1    1  0.00000  0.00000  0.00000
2    2  0.00000  0.00000  0.00000
3    3  0.00000  0.00000  0.00000
4    4 -0.03467 -0.03848 -0.02331
5    5  0.00000  0.00000  0.00000

if there are more spaces, then specify strip.white = TRUE

cbind(data[-2], read.table(text = data$leg1, header = FALSE, strip.white = TRUE))

CodePudding user response:

You can try to iterate through the string and check every char if it is a blank space.

code:

    public static void main (String[] args){
    String oldString = "a   b       c     d           e   f";

    StringBuilder newString = new StringBuilder();
    for(char c : oldString.toCharArray()){
        if(c != ' '){
            newString.append(c);
        }
    }
    System.out.printf("\n old String:%s \n new String: %s \n", oldString, newString.toString());
}

output:

 old String:a   b       c     d           e   f 
 new String: abcdef 

Then, you can check if the last char you checked was a whitespace. If yes, you copy your string newString into a list and clear your string newString. if not, you add the char to your string newString. newString functions as a buffer in this case and the list "strings" contains the strings you need.

Code:

public static void main (String[] args){
    String oldString = "abc   def       ghi     jkl           mno   pqr";


    StringBuilder newString = new StringBuilder();
    ArrayList<String> strings = new ArrayList<>();

    //lws = lastWasWhitespace
    boolean lws = false;
    for(char c : oldString.toCharArray()){
        if(c != ' '){
            if(lws){
                strings.add(newString.toString());
                newString.delete(0,newString.length());
            }
            newString.append(c);
            lws = false;
        }else{
            lws = true;
        }
    }
    System.out.printf("\n old String:%s \n",oldString );
    System.out.println("new Strings:");
    for(String string : strings){
        System.out.println(string);
    }
}

Output:

old String: abc   def       ghi     jkl           mno   pqr 
new Strings:
abc
def
ghi
jkl
mno

I hope it helped you.

CodePudding user response:

You can use str_split_fixed from the stringr package, after first replacing multiple spaces with 1:

data <- data.frame(name = c(1,2,3,4,5))

data$leg1 <- c(
  "0.00000  0.00000  0.00000",
  "-0.00000  -0.00000  -0.00000",
  "0.00000  0.00000  0.00000",
  "-0.03467  -0.03848  -0.02331",
  "-0.00000  -0.00000  -0.00000")

data$leg1 <- gsub("\\s ", " ", str_trim(data$leg1))

str_split_fixed(data$leg1, " ", 3) %>% 
  data.frame() %>% 
  rename(v1 = X1, v2 = X2, v3 = X3) %>% 
  cbind(data, .)

##   name                         leg1       v1       v2        v3
## 1    1    0.00000  0.00000  0.00000  0.00000  0.00000   0.00000
## 2    2 -0.00000  -0.00000  -0.00000 -0.00000 -0.00000  -0.00000
## 3    3    0.00000  0.00000  0.00000  0.00000  0.00000   0.00000
## 4    4 -0.03467  -0.03848  -0.02331 -0.03467 -0.03848  -0.02331
## 5    5 -0.00000  -0.00000  -0.00000 -0.00000 -0.00000  -0.00000
  • Related