Question: I have a dataframe with missing values. How can I write either a python or an R code to replace empty spaces with 0, a single string with 1, and multiple strings joined by "\t" with a number corresponding to how many "\t"s + 1.

my data frame:

```
        col1    col2    col3
row1    5blue   2green5 white
row2            white   green\twhite3\t3blue5
row3    blue3           white
row4    7blue   green2  
row5            3green  3white6
row6    6blue   green\t6white7  green   
row7    5blue5  6green  white
row8    blue6

```

Output expected:

```
        col1    col2    col3
row1    1   1   1
row2    0   1   3
row3    1   0   1
row4    1   1   0   
row5    0   1   1
row6    1   2   1   
row7    1   1   1
row8    1   0   0   

```

Any ideas? Thanks

Answer: 
I'm using a function that goes to each column element and checks if the element is a space (You can change that depending on what you have. It looks likes a space to me) and returns 0 if it is, otherwise it splits the string by "\t" and counts the strings that are produced.

```
# example dataset
dt = data.frame(col1 = c("green\twhite3\t3blue5","green"),
                col2 = c(" ", "green\twhite3"), stringsAsFactors = F)

dt

#                   col1         col2
# 1 green\twhite3\t3blue5             
# 2               green green\twhite3

ff = function(x) 
{
  res = vector()                                                             # create an empty vector to store counts for each element
  for (i in 1:length(x)){                                                    # iterate through each element
        res[i] = ifelse(x[i]==" ", 0, length(unlist(strsplit(x[i],"\t"))))   # if the element is space return 0, else split string by \t and count new strings
                        }
  return(res)                                                                # return the stored values
}

data.frame(sapply(dt, function(x) ff(x)))                                    # apply the function to all columns and save it as a data.frame

#     col1 col2
# 1    3    0
# 2    1    2

```
Use the `yourstring.count("\t")` function to get the number of tabs, add 1 to the value to get the number of words. If string is empty, output 0.