Reputation: 363
How to read the below stdin values and create a data frame in R. Here stdin values are dynamic
PrimeSuperMarket
4
Choclate
Jam
Milk
Rice
Line 1 denotes ShopName
Line 2 denotes NumberOfItems Planning to purchase
Line 3 to 7 : Item Type
Line No 3 to n is dynamic that depends upon line no 2 value. If value is passed as 5 in line 2. Line 3 to Line 8 will be there, So totally 5 values will be entered.
My Expected output is
Upvotes: 0
Views: 166
Reputation: 389135
If your input file is called input.txt
you can read it with readLines
and create a dataframe like so -
val <- readLines('input.txt')
shopName <- val[1]
Item <- val[3:(3 + as.numeric(val[2]) - 1)]
# Or you can read everything removing 1st 2 values
#Item <- val[-c(1:2)]
data.frame(shopName, Item)
# shopName Item
#1 PrimeSuperMarket Choclate
#2 PrimeSuperMarket Jam
#3 PrimeSuperMarket Milk
#4 PrimeSuperMarket Rice
Upvotes: 1