Home > Blockchain >  selecting and handling a row by looking its name in data [R]
selecting and handling a row by looking its name in data [R]

Time:12-31

I'm trying to get a specifically named row from data and I want to make plots with that row. However, I could not manage to select that row. I'm selecting but in that case, it gives NA for that row. Here is my code:

attach(climate_change)
turkey <- climate_change$"Country Name"[c("Turkey")]
print(turkey)

Here is output:

     > print(turkey)
    NA

How can I fix this? How can I get the specific row and handle it? Thanks for any help!

CodePudding user response:

climate_change[climate_change$`Country Name` == 'Turkey', ]

or

subset(climate_change, `Country Name` == 'Turkey')

or

climate_change[grep('Turkey', climate_change$`Country Name`), ]

gives:

#   Country Name x
# 1       Turkey 1
# 2       Turkey 2

Notes

  1. Beware of attach()! There are better alternatives
  2. Avoid spaces in variable names to avoid using quotes or backticks after the $. You may easily clean your names by doing names(climate_change <- make.names(names(climate_change))

Data:

climate_change <- structure(list(`Country Name` = c("Turkey", "Turkey", "Greece", 
"Greece", "Tuvalu", "Tuvalu"), x = c(1L, 2L, 1L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
-6L))

CodePudding user response:

This will work and prints all data related to country turkey

climate_change%>%
group_by(country_name)%>%
filter(country_name=='turkey')
  • Related