Reputation: 926
I have these assignment instructions.
Print, on each line, each name followed by “|”, followed by the age. Create a list where members are two vectors. The first vector holds the first names. The members of the list should have the names - "first names" and "ages". Output this list in the following format.
If I'm not mistaken, three things are required for the setup. A list containing two vectors.
first_names <- c("Jim", "Frank", "Sally")
ages <- c("25", "34", "42")
list_data <- list(first_names, ages)
# Desired Output
Jim | 25
Frank | 34
Sally | 42
I am able to accomplish this with using just two vectors but not when they are included in a list. Am I understanding the instructions correctly?
My version with only two vectors works.
first_names <- c("Jim", "Frank", "Sally")
ages <- c("25", "34", "42")
if (length(first_names) == length(ages)) {
for (i in 1:length(first_names)) {
print(paste(first_names[i],ages[i],sep = " | "))
}
} else {
print("Error")
}
But the version with the list is where I need help.
first_names <- c("Jim", "Frank", "Sally")
ages <- c("25", "34", "42")
list_data <- list(first_names, ages) # list
if (length(list_data[1]) == length(list_data[2])) {
for (i in 1:length(list_data[1])) {
print(paste(list_data[1][i],list_data[2][i],sep = " | "))
}
} else {
print("Error")
}
Upvotes: 1
Views: 59
Reputation: 39647
You can use mapply
with paste
.
mapply(paste, list_data[1], list_data[2], sep = " | ")
# [,1]
#[1,] "Jim | 25"
#[2,] "Frank | 34"
#[3,] "Sally | 42"
Or without lines and cols using writeLines
:
writeLines(mapply(paste, list_data[1], list_data[2], sep = " | "))
#writeLines(paste(list_data[[1]], list_data[[2]], sep = " | ")) #Alternative
#Jim | 25
#Frank | 34
#Sally | 42
Or with your code adapted using [[
instead of [
:
if (length(list_data[1]) == length(list_data[2])) {
for (i in 1:length(list_data[[1]])) {
writeLines(paste(list_data[[1]][i],list_data[[2]][i],sep = " | "))
}
} else {
print("Error")
}
#Jim | 25
#Frank | 34
#Sally | 42
Or using sprintf
to reproduce the desired ouptup:
writeLines(sprintf(paste0("%-", max(nchar(list_data[[1]])), "s", " | %s"),
list_data[[1]], list_data[[2]]))
Jim | 25
Frank | 34
Sally | 42
Upvotes: 1
Reputation: 388817
paste
is vectorized you don't need for
loop here.
cat(paste(first_names, ages, sep = " | ", collapse = '\n'))
#Jim | 25
#Frank | 34
#Sally | 42
If you want to use these tables in some reports there are better ways available with kable
or similar packages.
If we want to use list_data
we can do :
cat(paste(list_data[[1]], list_data[[2]], sep = " | ", collapse = '\n'))
Upvotes: 1
Reputation: 27732
library( data.table )
lapply( data.table::transpose(list_data), paste0, collapse = "|" )
[[1]]
[1] "Jim|25"
[[2]]
[1] "Frank|34"
[[3]]
[1] "Sally|42"
Upvotes: 1
Reputation: 653
Well , you can use inbuilt function paste
or paste0
in R.
Nothing else required
paste0(first_names,"|",ages)
The above code will print you the desired result.
To store it in a list , you can use
as.list(paste0(first_names,"|",ages))
Upvotes: 0