Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

csv - How to include a new column when using base R?

I have a csv file as 'Campaigname.csv'

AdvertiserName,CampaignName
Wells Fargo,Gary IN MetroChicago IL Metro
EMC,Los Angeles CA MetroBoston MA Metro
Apple,Cupertino CA Metro

Desired Output in R

AdvertiserName,City,State
Wells Fargo,Gary,IN
Wells Fargo,Chicago,IL
EMC,Los Angeles,CA
EMC,Boston,MA
Apple,Cupertino,CA

The code to the solution was given in a previous stackoverflow answer as:

## read the csv file - modify next line as needed
xx <- read.csv("Campaignname.csv",header=TRUE)

s <- strsplit(xx$CampaignName, " Metro")
names(s) <- xx$Market
ss <- stack(s)
DF <- with(ss, data.frame(Market = ind, 
City = sub(" ..$", "", values),
State = sub(".* ", "", values)))

write.csv(DF, file = "myfile.csv", row.names = FALSE, quote = FALSE)

But now another column like 'Identity' is included where the input is

Market,CampaignName,Identity
Wells Fargo,Gary IN MetroChicago IL Metro,56
EMC,Los Angeles CA MetroBoston MA Metro,78
Apple,Cupertino CA Metro,68

And the desired result is

 Market,City,State,Identity
 Wells Fargo,Gary,IN,56
 Wells Fargo,Chicago,IL,56
 EMC,Los Angeles,CA,78
 EMC,Boston,MA,78
 Apple,Cupertino,CA,68

The number of columns may not be limited to just 3 columns, it may keep on increasing.

How to do it in R? New to R.Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure that I fully understand your question, and you didn't provide a reproducible example (so I can't run your code and try to get to the end point you want). But I'll still try to help.

Generally speaking, in R you can add a new column to a data.frame simply by using it.

df = data.frame(advertiser = c("co1", "co2", "co3"),
                campaign   = c("camp1", "camp2", "camp3"))
df
  advertiser campaign
1        co1    camp1
2        co2    camp2
3        co3    camp3

At this point, if I wanted to add an identity column I would simply create it with the $ operator like this:

df$identity = c(1, 2, 3)
df
  advertiser campaign identity
1        co1    camp1        1
2        co2    camp2        2
3        co3    camp3        3

Note that there are other ways to accomplish this - see the transform (?transform) and rbind (?rbind) functions.

The caveat when adding a column to a data.frame is that I believe you must add a vector that has the same number of elements as their are rows in the data.frame. You can see the number of rows in the data.frame by typing nrow(df).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...