- 1
Nickname Maker (R programming)
The given code defines a dataframe with names and years of birth of people. You need to make a program that takes an index as input, and outputs the nickname of the person at that index in the dataframe. The nickname should combine the name and the year together. For example, the first person's nickname should be James1988. and this for code index <- readLines('stdin') index <- as.integer(index[1]) x <- data.frame( "name" = c("James", "Amy", "David", "Bob", "John"), "year" = c("1988","2001", "1996", "2004", "1999")) how to solve this ?
4 Respostas
0
* use the $ syntax to get the column
* use input index to get the element of the column
* concatenate name and year using the paste0 function
* output the result
0
oke Lisa thanks, i will try to solve the problem with your tips
0
To get the right answer:
1. Specify your result, using paste and the sep function
e.g result = paste (x$name, x$year, sep ='') (no space in sep because we don't need space in our output)
2. Equate the data frame variable to the 'result' of the input variable
e.g x = result[index]
3. print ( x)
Hope this explains it.
0
index <- readLines('stdin')
index <- as.integer(index[1])
x <- data.frame(
"name" = c("James", "Amy", "David", "Bob", "John"),
"year" = c("1988","2001", "1996", "2004", "1999"))
print(paste(x[[index,1]], x[[index,2]], sep=""))
#its work...