1

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!

camille
  • 16,432
  • 18
  • 38
  • 60

2 Answers2

1

This will work and prints all data related to country turkey

climate_change%>%
group_by(country_name)%>%
filter(country_name=='turkey')
Sravan
  • 11
  • 2
1
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))
jay.sf
  • 60,139
  • 8
  • 53
  • 110