f <- function (x) {
isState <- is.element(tolower(x), tolower(state.name))
w <- which(isState)
data.frame(State = x[rep(w, diff(c(w, length(x) + 1)) - 1L)],
City = x[!isState])
}
E.g.,
V1 <-c("alabama", "bates", "tuscaloosa", "smith", "arkansas", "fayette",
"little rock", "alas
es, word)
}
}
print(data.frame(V1=states, V2=cities))
#
-John
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David
> Winsemius
> Sent: Sunday, January 04, 2015 2:48 AM
> To: npretnar
> Cc: R-help@r-project.o
On Jan 3, 2015, at 9:20 PM, npretnar wrote:
> Sorry. Bad example on my part. Try this. V1 is ...
>
> V1
> alabama
> bates
> tuscaloosa
> smith
> arkansas
> fayette
> little rock
> alaska
> juneau
> nome
>
> And I want:
>
> V1V2
> alabama bates
> alabama tuscaloo
Sorry. Bad example on my part. Try this. V1 is ...
V1
alabama
bates
tuscaloosa
smith
arkansas
fayette
little rock
alaska
juneau
nome
And I want:
V1 V2
alabama bates
alabama tuscaloosa
alabama smith
arkansasfayette
arkansaslittle rock
alaska juneau
al
I'm not sure what's so complicated about that (am I missing
something?). You can search using grep, and replace using gsub, so
tmpDF <- read.table(text="V1 V2
A 5
a1 1
a2 1
a3 1
a4 1
a5 1
B 4
b1 1
b2 1
b3 1
b4 1",
he
I have a string variable (V1) in a data frame structured as follows:
V1 V2
A 5
a1 1
a2 1
a3 1
a4 1
a5 1
B 4
b1 1
b2 1
b3 1
b4 1
I want the following:
V1 V2 V3
a1 1 A
a2 1 A
a3
6 matches
Mail list logo