I am trying to simply use rbind for two columns and I use the following (all variables are city names and r considers them as factor)
firstcitynames <- rcffull$X1CityName
secondcitynames <- rcffull$X2CityName
allcitynames <- rbind(firstcitynames, secondcitynames)
allcitynames
then when get to View(allcitynames) all I get is a bunch of numbers instead of names:
[,2276] [,2277] [,2278] [,2279] [,2280] [,2281]
[,2282] [,2283] [,2284] [,2285] [,2286] [,2287]
Any suggestions?
You need to convert factors to characters with as.character(df$var)
Here's an illustration
a <- factor(letters[1:10])
b <- factor(LETTERS[1:10])
rbind(a,b)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## a 1 2 3 4 5 6 7 8 9 10
## b 1 2 3 4 5 6 7 8 9 10
rbind(as.character(a), as.character(b))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## [2,] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
Assuming firstcitynames and secondcitynames are of type factors
you can try this
rbind(levels(firstcitynames),levels(secondcitynames))
this one also worked:
firstcitynames <- as.tibble(rcffull$X1CityName)
secondcitynames <- as.tibble(rcffull$X2CityName)
allcitynames <- rbind(firstcitynames, secondcitynames)
allcitynames
Related
Think I have an easy question but I was not able to find my mistake so far.
I want to create a single elimination tournament with 32 participants. To do so I created 4 groups with 8 Players and now a want to build the matchups with a for loop and store them into a list, but its not working as I want :(
>groups
A B C D
1 Player1 Player9 Player17 Player25
2 Player2 Player10 Player18 Player26
3 Player3 Player11 Player19 Player27
4 Player4 Player12 Player20 Player28
5 Player5 Player13 Player21 Player29
6 Player6 Player14 Player22 Player30
7 Player7 Player15 Player23 Player31
8 Player8 Player16 Player24 Player32
now I want to store them as pairs in their groups to have an easy access to calculate winning props later on.
Should look like this for the first Group A
>teams.group.A <- matrix(groups$A,nrow = 2,ncol = 4)
>teams.group.A
[,1] [,2] [,3] [,4]
[1,] "Player1" "Player3" "Player5" "Player7"
[2,] "Player2" "Player4" "Player6" "Player8"
my idea for the loop was:
groupnames <- colnames(groups)
mylist <- list()
for(i in groupnames){
mylist[[i]] <- matrix(groups$i,nrow = 2,ncol = 4)
}
mylist
I get the error, that "data" must be a vector type?
Thx, if you can help me here!
Assuming your groups variable is a dataframe, you can easily use lapply and you'll get a nice named list as a result:
# generate data
players <- paste0('Player',1:32)
grps <- data.frame(A=players[1:8],B=players[9:16],C=players[17:24],D=players[25:32])
#smoother version as suggested by P Lapointe:
mylist <- lapply(grps,matrix,nrow=2)
# more verbose
# mylist <- lapply(grps,function(x) matrix(x,ncol = 4))
# output
> mylist
$A
[,1] [,2] [,3] [,4]
[1,] "Player1" "Player3" "Player5" "Player7"
[2,] "Player2" "Player4" "Player6" "Player8"
$B
[,1] [,2] [,3] [,4]
[1,] "Player9" "Player11" "Player13" "Player15"
[2,] "Player10" "Player12" "Player14" "Player16"
$C
[,1] [,2] [,3] [,4]
[1,] "Player17" "Player19" "Player21" "Player23"
[2,] "Player18" "Player20" "Player22" "Player24"
$D
[,1] [,2] [,3] [,4]
[1,] "Player25" "Player27" "Player29" "Player31"
[2,] "Player26" "Player28" "Player30" "Player32"
To bring it back into a single dataframe, you can use do.call(cbind,mylist):
> do.call(cbind,mylist)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] "Player1" "Player3" "Player5" "Player7" "Player9" "Player11" "Player13" "Player15" "Player17" "Player19" "Player21"
[2,] "Player2" "Player4" "Player6" "Player8" "Player10" "Player12" "Player14" "Player16" "Player18" "Player20" "Player22"
[,12] [,13] [,14] [,15] [,16]
[1,] "Player23" "Player25" "Player27" "Player29" "Player31"
[2,] "Player24" "Player26" "Player28" "Player30" "Player32"
You can't index with i in the loop using $, because i is a character. Try this
for (i in groupnames) {
mylist[[i]] = matrix(groups[[i]], nrow=2, ncol=4)
}
I need help in how to manage lists in an iterative way.
I have the following list list which is composed of several dataframes with same columns, but different number of rows. Example:
[[1]]
id InpatientDays ERVisits OfficeVisits Narcotics
1 a 0 0 18 1
2 b 1 1 6 1
3 c 0 0 5 3
4 d 0 1 19 0
5 e 8 2 19 3
6 f 2 0 9 2
[[2]]
id InpatientDays ERVisits OfficeVisits Narcotics
7 a 16 1 8 1
8 b 2 0 8 0
9 c 2 1 4 3
10 d 4 2 0 2
11 e 6 5 20 2
12 a 0 0 7 4
I would like to apply a function to get all the possible combinations for the id for each "data frame" in the list.
I intended to try something like this lapply(list1, function(x) combn(unique(list1[x]$id))) Which of course does not work.. expecting to get something like:
[[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] "a" "a" "a" "a" "a" "b" "b" "b" "b" "c" "c" "c" "d" "d" "e"
[2,] "b" "c" "d" "e" "f" "c" "d" "e" "f" "d" "e" "f" "e" "f" "f"
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d"
[2,] "b" "c" "d" "e" "c" "d" "e" "d" "e" "e"
Is this possible? I know for sure this works for a single dataframe df
combn(unique(df$id),2)
We need to use unique(x$id)
lapply(list1, function(x) combn(unique(x$id),2))
The OP's code is looping the 'list1' using lapply. The anonymous function call (function(x)) returns each of the 'data.frame' within the list i.e. 'x' is the 'data.frame'. So, we just need to call x$id (or x[['id']]) to extract the 'id' column. In essence, 'x' is not an index. But, if we need to subset based on the index, we have to loop through the sequence of 'list1' (or if the list elements are named, then loop through the names of it)
lapply(seq_along(list1), function(i) combn(unique(list1[[i]]$id), 2))
I have large list of matrix data that looks like this:
$`1`
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
2010 "6 811 529 000" NA NA NA "455 782 000"
2011 "7 531 264 000" NA NA NA "585 609 000"
2012 "8 013 843 000" NA NA NA "702 256 000"
and I would like to replace the NA with empty string like this : ""
The solution must be without conversion to data.frame since this: x[is.na(x)] <- ""
would solve the issue.
This works for me: print(x, na.print = "") but I cannot figure it out how to store the print output.
You can do this with lapply:
# Setup sample data frame
dat = list(matrix(c(NA, "a", "b", NA), nrow=2),
matrix(c(rep("r", 8), NA), nrow=3))
dat
# [[1]]
# [,1] [,2]
# [1,] NA "b"
# [2,] "a" NA
#
# [[2]]
# [,1] [,2] [,3]
# [1,] "r" "r" "r"
# [2,] "r" "r" "r"
# [3,] "r" "r" NA
# Do conversion
dat <- lapply(dat, function(x) { x[is.na(x)] <- "" ; x })
dat
# [[1]]
# [,1] [,2]
# [1,] "" "b"
# [2,] "a" ""
#
# [[2]]
# [,1] [,2] [,3]
# [1,] "r" "r" "r"
# [2,] "r" "r" "r"
# [3,] "r" "r" ""
I would like to add the individual list name to the last column, respectively. what is the best way to do that efficiently.
lst <- list(a=matrix(runif(10), nrow=5, ncol=2), b=matrix(runif(6), nrow=3, ncol=2))
$a
[,1] [,2]
[1,] 0.5257330 0.52673079
[2,] 0.2103107 0.23357179
[3,] 0.3745236 0.03687697
[4,] 0.9731074 0.15569480
[5,] 0.2248541 0.60258915
$b
[,1] [,2]
[1,] 0.9901820 0.3648310
[2,] 0.8922225 0.4285105
[3,] 0.6963518 0.5795353
I would like this one: it means the individual list name should be added in the last column, respectively.
$a
[,1] [,2] [,3]
[1,] "0.52573303761892" "0.526730791199952" "a"
[2,] "0.210310699883848" "0.233571790158749" "a"
[3,] "0.374523550504819" "0.0368769748602062" "a"
[4,] "0.973107369150966" "0.155694802291691" "a"
[5,] "0.224854125175625" "0.602589153219014" "a"
$b
[,1] [,2] [,3]
[1,] "0.990182007197291" "0.36483103595674" "b"
[2,] "0.892222490161657" "0.42851050500758" "b"
[3,] "0.696351842954755" "0.579535307129845" "b"
Any help will be appreciated.
Kevin
A solution that keeps the names from the original list:
mapply(function(x, y) cbind(x, y), lst, names(lst))
Here's a solution that gives you exactly what you asked for. Based on your expected output, it seems like you're aware that by doing so, you're coercing the numbers in the matrix to characters.
lapply(names(lst), function(x) {
`colnames<-`(cbind(lst[[x]], x), NULL)
} )
# [[1]]
# [,1] [,2] [,3]
# [1,] "0.497699242085218" "0.934705231105909" "a"
# [2,] "0.717618508264422" "0.212142521282658" "a"
# [3,] "0.991906094830483" "0.651673766085878" "a"
# [4,] "0.380035179434344" "0.125555095961317" "a"
# [5,] "0.777445221319795" "0.267220668727532" "a"
#
# [[2]]
# [,1] [,2] [,3]
# [1,] "0.386114092543721" "0.86969084572047" "b"
# [2,] "0.0133903331588954" "0.34034899668768" "b"
# [3,] "0.382387957070023" "0.482080115471035" "b"
I am trying to get all the possible combinations of length 3 of the elements of a variable. Although it partly worked with combn() I did not quite get the output I was looking for. Here's my example
x <- c("a","b","c","d","e")
t(combn(c(x,x), 3))
The output I get looks like this
[,1] [,2] [,3]
[1,] "a" "b" "c"
[2,] "a" "b" "d"
[3,] "a" "b" "e"
I am not really happy with this command for 2 reasons. I wanted to get an output that says "a+b+c" "a+b+b"...., unfortunately I wasn't able to edit the output with paste() or something.
I was also looking forward for one combination of each set of letters, that is I either get "a+b+c" or "b+a+c" but not both.
Try something like:
x <- c("a","b","c","d","e")
d1 <- combn(x,3) # All combinations
d1
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
# [2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
# [3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
nrow(unique(t(d1))) == nrow(t(d1))
# [1] TRUE
d2 <- expand.grid(x,x,x) # All permutations
d2
# Var1 Var2 Var3
# 1 a a a
# 2 b a a
# 3 c a a
# 4 d a a
# 5 e a a
# 6 a b a
# 7 b b a
# 8 c b a
# 9 d b a
# ...
nrow(unique(d2)) == nrow(d2)
# [1] TRUE
try this
x <- c("a","b","c","d","e")
expand.grid(rep(list(x), 3))