r - Adding multiple columns to a data frame -


मेरे पास एक डेटा फ्रेम है df जैसे:

    

अब एक विशिष्ट पंक्ति के काम करने के लिए एक एकल कॉलम जोड़ना:

  डीएफ [4, "डी"] & lt; - 5 डीएफ # एबीडी # 1 1 101 एनए # 2 2 102 एनए # 3 3 103 एनए # 4 4 104 5 # 5 5 105 एनए   

लेकिन कई कॉलम एक साथ जोड़ नहीं!

  df [3, c ("b", "f", "g")] & lt; - c (6,7, 8) # त्रुटि `* टीएमपी * '[[जे]]: पुनरावर्ती अनुक्रमण स्तर 2 पर असफल रहा   

क्या देता है?

इसे प्राप्त करने का लंबा रास्ता, अर्थात, है:

  df [3, "b"] & lt; - 6 df [3, "f"] & lt; 7 df [3, "g"] & lt; - 8   

लेकिन यह वास्तव में सबसे आसान (और सही) तरीका है?

इस प्रकार प्राप्त करने का एक और स्वचालित तरीका निम्नानुसार है:

  df & lt; - data.frame (a = 1: 5, b = 101: 105) cols & lt; -c ("ई", "एफ", "जी") vals & lt; - c (6, 7, 8) sapply (1: लंबाई (कॉलम), फ़ंक्शन (i) df [3, cols [i]] & lt; & lt ; - vals [i]) df   

जो देता है:

  abefg 1 1 101 NA NA NA 2 2 102 NA NA NA 3 3 103 6 7 8 4 4 104 एनए एनए 5 5 105 एनए एनए    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -