Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How do I create a new column and add text that is specific to each row in R studio?

$
0
0

I'm new to R Studio and am learning about dataframes. I'm trying to add the new column "uniqueID" to my dataframe "Populations" with unique values for each row in this new column. No problem, I can append a new column like this: Populations$uniqueID

However I'm having trouble adding unique values to each row under this new column. The values should be a combination of the values in each row from the existing columns "location", "variant", and "time". So, for each row the value for the new column uniqueID should be something like "LocationVariantTime" (e.g. "CaliforniaMedium1953"). Here's the code I'm trying, using paste(), but it's definitely wrong. I need to figure out how to grab the values for each row.

Populations$uniqueID <- paste(Populations$location, Populations$variant, Populations$time)

Here's the output when I view the dataframe. There is no new column with data: https://share.getcloudapp.com/7Kuykdg4

The error that I get reads:

Error in $<-.data.frame(*tmp*, uniqueID, value = character(0)) : replacement has 0 rows, data has 280932

Thank you in advance for helping someone who is learning,


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>