[R] Relist strings? [Was: How to remove square brackets, etc. from address strings?]

Rui Barradas ruipbarradas at sapo.pt
Tue May 29 23:11:31 CEST 2012


What is the result after each step? Could you use dput to post them?

dput(head(r))
dput(head(r1))  # after the first lapply

Copy the output of those instructions and paste them here.
I'm asking this because I've tried with your dataset and it worked.

Rui Barradas


Em 29-05-2012 21:06, Sabina Arndt escreveu:
> Hello,
>
> thank you very much for your reply, Rui Barradas.
>
>> In my workspace everything was converted either to non-empty strings or
>> NULLs.
>> This is how to do it.
>>
>>
>> r1<- lapply(r, function(x) x[nchar(x)>  0])
>> r1<- lapply(r1, function(x) if(length(x)) x else NULL)  # second pass
>> country.list<- r1[ -which(sapply(r1,  is.null)) ]
>> country.list
> Thank you.
> I tried it but I've still got the same result as before, unfortunately:
>> x<- readLines("sabina.txt")
>> s<- strsplit(x, ";[[:space:]]\\[")
>> r<- lapply(s, function(x) sapply(strsplit(x, "[[:blank:]]"), tail, 1))
>> length(r)
> [1] 20
>> r[[20]]<- NULL
>> r[[19]]<- r[[19]][ -length(r[[19]]) ]
>> r1<- lapply(r, function(x) x[nchar(x)>  0])
>> r1<- lapply(r1, function(x) if(length(x)) x else NULL)  # second pass
>> country.list<- r1[ -which(sapply(r1,  is.null)) ]
>> country.list
> list()
>
>> After removing the nulls, in my workspace the list numbers are
>> different, but you could remove unwanted values along the lines of
>>
>> bad<- -length(r[[18]])
>> r[[18]]<- r[[18]][ -bad ]
>>
>> Note that you could do this to 'country.list', it might be simpler.
> OK, which step would this be in the order above?
> And how / where do I get the country name that was incorrectly replaced by the Article Identifier?
>
>> If it all works correctly, adjustments can be made, if not it might be
>> premature. I don't know.
> I see.
>
>> See how it goes, so far.
> See above, please.
>
>> You're welcome,
> Thank you!
>
> Faithfully yours,
>
> Sabina Arndt
>
> PS: I cut the old messages to save list space.
>



More information about the R-help mailing list