Home:ALL Converter>looping to concatenate string

looping to concatenate string

Ask Time:2016-08-09T19:04:58         Author:Azam Yahya

Json Formatter

Sorry for asking a basic question. I've tried looking for the answer in the following links but with no luck

How to concatenate strings in a loop?

How to concatenate strings in a loop?

C concatenate string with int in loop

So, here is a reproducible example. I've a list called house i.e

house <- c("Dining Room", "Drawing Room", "Number of Bathrooms", "5", "Number of Bedroom", "5", "Number of Kitchens", "1")

where every element in the house list is character. Now I want to create another list where if the element of list is of character length of one(representing a number) then it should concatenate with the previous string element. This is output I am expecting.

"Dining Room", "Drawing Room", "Number of Bathrooms 5", "Number of Bedroom 5", "Number of Kitchens 1"

I have tried running a loop but the output is not similar to what I expect.

for(i in house){ if(!is.na(nchar(house[i])) == 1) { cat(i,i-1) } else{ print(i) } }

Author:Azam Yahya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38849019/looping-to-concatenate-string
yy