1
I have the following scenario:
Data frame - Resources:
recursos <- as.data.frame(c("server01","server02","server03"));
colnames(recursos) <- "Server"
Data frame - Events:
eventos <- as.data.frame(c("falha no server01","erro no server01","falha no server02","erro no server03"));
colnames(eventos) <- "Eventos";
eventos <- dplyr::mutate(eventos, Server = "")
My object is popular the Server column in the data frame events, using as a basis the servers mapped in the Resource data frame. In the solution below, I can fill in using a unique value. But that’s not what I need yet.
eventos$Server <- ifelse(grepl("server01", eventos$Eventos), "server01", "")
I thought about running a for(server in recursos$Server), changing the grep, for a variable. But I could not do it this way. Someone could help me ?
Thanks in advance.
Erikson, could I replace the regular expression with a column with multiple servers ? I ask this because the name of my servers, are random, some start with s, others with x, others with d. They are several names of servers. All are in the resources$Servers.
– Vitor Quix
Vitor, if the last name in the events column is the server name you can use this solution adapted from the Erikson solution
dplyr::mutate(eventos, Server = regmatches(Eventos, regexpr('\\w+$', Eventos)))
– Daniel Ikenaga
Note that none of the solutions are being analyzed if the server belongs to
recursos
. This could cost you more processing if you don’t need it.– Daniel Ikenaga
Daniel, the problem is I have 200 different server names. Some starting with X, others with Y, others with B. So I stored the global server list in the dataframe resources, Servers column. The Erikson solution is very good. I will store this code for future needs. But for my scenario, I only need to use this list of servers in regmatches.
– Vitor Quix
Unfortunately, it is only possible to use a regular expression in this case. My suggestion would be to create a more generic regular expression, which covers all cases, or use the function
strsplit
to compare the terms with the list indata.frame
. I shall amend the reply to add that suggestion.– Erikson K.
Erikson, assuming one of the examples in the dataframes are these: events[3,] - > "THE MEMORY HEALTH OF HOST D715WS074 IS NOT OK". resources[7,] -:> "D715WS074". You need to change something in your code ?
– Vitor Quix
You do not need to change anything. If there is a possibility of
Eventos
are not inrecursos
, it is necessary to foresee a standard case.– Erikson K.